Document

SUBSCRIBE TO GET FULL ACCESS TO THE E-BOOKS FOR FREE 🎁SUBSCRIBE NOW

Professional Dropdown with Icon

SUBSCRIBE NOW TO GET FREE ACCESS TO EBOOKS

file module in ansible deals with file/directory-related operations.

Example 1:- 

Change file permission and ownership

Playbook: fileper.yaml— – name: play for file module hosts: webservers tasks: – name: Change file permssion file: path: /tmp/foo.conf owner: vagrant group: vagrant mode: 0644

Execute:-    ansible-playbook fileper.yaml

Example 2:- 

Create a soft link file

Playbook: fileper.yaml— – name: play for file module hosts: webservers tasks: – name: Create a soft link file file: src: /tmp/foo.conf dest: /tmp/foo.lnk state: link

Execute:-    ansible-playbook fileper.yaml

Example 3:- 

Create a soft link file with loop

Playbook: fileper.yaml— – name: play for file module hosts: webservers tasks: – name: Create a soft link file file: src: ‘/tmp/{{ item.src }}’ dest: ‘{{ item.dest }}’ state: link with_items: – { src: ‘foo.conf’, dest: ‘foo.lnk’ } – { src: ‘foo.txt’, dest: ‘footxt.lnk’ }

Execute:-    ansible-playbook fileper.yaml

Example 4:- 

Change the permission of file using symbolic modes

Playbook: fileper.yaml— – name: play for file module hosts: webservers tasks: – name: Create a soft link file file: path: /tmp/newfile.txt state: touch mode : u=rw,g=r,o=r

Execute:-    ansible-playbook fileper.yaml

Example 5:- 

Change the permission of file using symbolic modes

Playbook: fileper.yaml— – name: play for file module hosts: webservers tasks: – name: Create a soft link file file: path: /tmp/newfile.txt state: touch mode : u+rw,g+r,o-r

Execute:-    ansible-playbook fileper.yaml

Example 6:- 

Preserve modification and access time of a file with state= touch

Playbook: fileper.yaml— – name: play for file module hosts: webservers tasks: – name: Create a soft link file file: path: /tmp/foo.conf state: touch modification_time: “preserve” access_time: “preserve”

Execute:-    ansible-playbook fileper.yaml

Example 7:- 

Change modification and access time of a file with current date time during state=touch

Playbook: fileper.yaml— – name: play for file module hosts: webservers tasks: – name: Create a soft link file file: path: /tmp/foo.conf state: touch modification_time: now access_time: now

Execute:-    ansible-playbook fileper.yaml

Example 8:- 

Create a directory

Playbook: fileper.yaml— – name: play for file module hosts: webservers tasks: – name: Create a soft link file file: path: /tmp/ansibledir state: directory

Execute:-    ansible-playbook fileper.yaml