• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

How to set permissions to the files by using Ansible playbook

  • 00:41 lsb_release -a
  • 00:58 vi test.yml
  • 03:31 ansible-playbook test.yml --syntax-check
  • 03:53 ansible-playbook test.yml
  • 04:13 ls -la /
  • 04:36 vi test.yml
  • 06:23 ansible-playbook test.yml --syntax-check
  • 06:37 ansible-playbook test.yml
{{postValue.id}}

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

Tags:
philippe
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

What is the module used for Linux file management in Ansible?

A

"file" is the module used for Linux file management in Ansible.

Q

For what win_file Module is used?

A

"win_file" is used to create, touch, or remove files or directories.

Q

How to define a number of levels to descend under the "file" module?

A

Use "depth" to define the directory levels

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

What does Set_fact do in Ansible?

A

It is used to gather information from your remote hosts.

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Isaac ?
How to run windows application in linux

I need to run the windows application in my Linux machine, instead of installing from yum repo or any other repos. How to do that..??

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.