Ansible Pilot

Add Execute Permission 755 Linux file - Ansible module file

How to automate the setting of execute permission 755 for example.sh Linux file with Ansible module file.

How to Add Execute Permission 755 on Linux file with Ansible?

I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible Add Execute Permission

Today we’re talking about the Ansible module file. The full name is ansible.builtin.file, which means that is part of the collection of modules “builtin” with ansible and shipped with it. It’s a module pretty stable and out for years. It works in a different variety of operating systems. It manages files and file properties. For Windows targets, use the ansible.windows.win_file module instead.

Main Parameters

This module has some parameters to perform any tasks. The only required is “path”, where you specify the filesystem path of the file you’re going to edit. The parameter “owner” set the user that should own the file/directory. The parameter “group” set the group that should own the file/directory. The parameter “mode” set the permissions in the UNIX way of the file/directory. The state defines the type of object we are modifying, the default is “file” but we could handle also directories, hard links, symlinks, or only update the access time with the “touch” option. Let me also highlight that we could also specify the SELinux properties.

demo

How to Add Execute Permission 755 file on Linux with Ansible Playbook. I’m going to show you how to set the chmod +x of an example.sh Linux file with Ansible.

code

---
- name: file module demo
  hosts: all
  vars:
    myscript: "~/example.sh"
  tasks:
    - name: set execution permission
      ansible.builtin.file:
        dest: "{{ myscript }}"
        mode: 'a+x'
#!/bin/bash
echo "Hello World"

execution

ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory file_management/file_permission.yml
PLAY [file module demo] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [set execution permission] *******************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

idempotency

ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory file_management/file_permission.yml
PLAY [file module demo] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [set execution permission] *******************************************************************
ok: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

before execution

ansible-pilot $ ssh [email protected]
Last login: Wed Mar 30 13:38:46 2022 from 192.168.0.59
[[email protected] ~]$ cat example.sh 
#!/bin/bash
echo "Hello World"
[[email protected] ~]$[[email protected] ~]$ ls -al example.sh 
-rw-r--r--. 1 devops wheel 31 Mar 30 13:39 example.sh
[[email protected] ~]$

after execution

ansible-pilot $ ssh [email protected]
Last login: Wed Mar 30 13:40:36 2022 from 192.168.0.59
[[email protected] ~]$ ls -al example.sh 
-rwxr-xr-x. 1 devops wheel 31 Mar 30 13:39 example.sh
[[email protected] ~]$ ./example.sh 
Hello World
[[email protected] ~]$ cat example.sh 
#!/bin/bash
echo "Hello World"

code with ❤️ in GitHub

Recap

Now you know how to Add Execute Permission 755 for a file on Linux. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 100+ Automation Examples For Linux and Windows System Administrator and DevOps

Want to keep this project going? Please donate

Trustpilot
Follow me

Subscribe not to miss any new releases

March 30, 2022

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress