How to set permissions to the files by using Ansible playbook

To Set Permissions to the files by using Ansible playbook

Introduction:

Ansible is a tool that automates the configuration, deployment, and maintenance of applications. The Linux operating system utilizes ownership and permissions for security. Linux systems have three types of users: Owners, Groups, and Others.

Prerequisite:

ansible

Configuration Steps:

Step 1: Check the OS version by using the following command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Step 2: Create playbook as test.yml for Creating a file named “Linuxhelp”

root@linuxhelp:~# vi test.yml
- hosts: localhost
  gather_facts: true
  tasks:
  - name: Create a file named Linuxhelp
    file:
       path: /Linuxhelp
       state: touch

Step 3: Check the syntax of the test.yml ansible playbook by using the following command

root@linuxhelp:~# ansible-playbook test.yml --syntax-check

playbook: test.yml

Step 4: Run the test.yml playbook by using the following command

root@linuxhelp:~# ansible-playbook test.yml 

PLAY [localhost] **********************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************
ok: [localhost]

TASK [Create a file named Linuxhelp] **************************************************************************************
changed: [localhost]

PLAY RECAP ****************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Step 5: List the files to view the created file

root@linuxhelp:~# ls -la /
total 1918400
drwxr-xr-x  20 root root       4096 Nov  9 02:48 .
drwxr-xr-x  20 root root       4096 Nov  9 02:48 ..
lrwxrwxrwx   1 root root          7 Sep  8 16:48 bin -> usr/bin
lrwxrwxrwx   1 root root          7 Sep  8 16:48 lib -> usr/lib
lrwxrwxrwx   1 root root          9 Sep  8 16:48 lib32 -> usr/lib32
lrwxrwxrwx   1 root root          9 Sep  8 16:48 lib64 -> usr/lib64
lrwxrwxrwx   1 root root         10 Sep  8 16:48 libx32 -> usr/libx32
-rw-r--r--   1 root root          0 Nov  9 02:47 Linuxhelp
drwx------   2 root root      16384 Sep  8 16:47 lost+found
drwxr-xr-x  35 root root        940 Nov  9 02:42 run

Step 6: Edit the test.yml playbook for Changing the permissions to the file named “Linuxhelp”

root@linuxhelp:~# vi test.yml 
- hosts: localhost
  gather_facts: true
  tasks:
  - name: Create a file named Linuxhelp
    file:
       path: /Linuxhelp
       state: touch
  - name: Changing the permissions	
    file:
       path: /Linuxhelp
       owner: root
       group: linuxhelp
       mode: 0755

Step 7: Again Check the syntax of the test.yml ansible playbook by using the following command

root@linuxhelp:~# ansible-playbook test.yml --syntax-check

playbook: test.yml

Step 8: Run the test.yml playbook by using the following command

root@linuxhelp:~# ansible-playbook test.yml 

PLAY [localhost] **********************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************
ok: [localhost]

TASK [Create a file named Linuxhelp] **************************************************************************************
changed: [localhost]

TASK [Changing the permissions] *******************************************************************************************
changed: [localhost]

PLAY RECAP ****************************************************************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Step 9: List the files to view the permissions and ownership changed for the file "Linuxhelp”

root@linuxhelp:~# ls -la /
total 1918400
drwxr-xr-x  20 root root            4096 Nov  9 02:48 .
drwxr-xr-x  20 root root            4096 Nov  9 02:48 ..
lrwxrwxrwx   1 root root               7 Sep  8 16:48 bin -> usr/bin
drwxr-xr-x   4 root root            4096 Nov  3 06:13 boot
lrwxrwxrwx   1 root root               7 Sep  8 16:48 lib -> usr/lib
lrwxrwxrwx   1 root root               9 Sep  8 16:48 lib32 -> usr/lib32
lrwxrwxrwx   1 root root               9 Sep  8 16:48 lib64 -> usr/lib64
lrwxrwxrwx   1 root root              10 Sep  8 16:48 libx32 -> usr/libx32
-rwxr-xr-x   1 root linuxhelp          0 Nov  9 02:57 Linuxhelp
drwx------   2 root root           16384 Sep  8 16:47 lost+found
drwxrwxrwt  18 root root            4096 Nov  9 02:57 tmp
drwxr-xr-x  14 root root            4096 Apr 20  2021 usr
drwxr-xr-x  14 root root            4096 Apr 20  2021 var

By this setting permissions to the files by using Ansible playbook comes to end

FAQ
Q
What does Set_fact do in Ansible?
A
It is used to gather information from your remote hosts.
Q
What is the copy module in Ansible?
A
The copy module is used to copy files and folders from the local machine to the remote servers
Q
How to define a number of levels to descend under the "file" module?
A
Use "depth" to define the directory levels
Q
For what win_file Module is used?
A
"win_file" is used to create, touch, or remove files or directories.
Q
What is the module used for Linux file management in Ansible?
A
"file" is the module used for Linux file management in Ansible.