Ansible File Module

0 Comment

Ansible File Module:

ansible file module is used for creating files and deleting the files in the remote server. With ansible file module We can also create multiple files and we can also delete multiple files in the remote server. we can create files with content.  we can create directories we can delete directories and we can change the permissions of the files. We can create symlinks or soft links and we can create hard links and we can delete those links. With the ansible file module, we can set the permissions to the files.

creating a file in remote server-ansible file module

in ansible file module we have different parameters. In this case, we are using path and state parameters. in every file module path is a must. In the path parameter, we will mention the path of the file in remote server. At this path only the file will be created.

at path:  mention the path of the file in remote server.

at state:  mention touch,   touch will create file exact like Linux command.

it will create a new empty file with name devops.txt. so mention filename in the path.

so in the state: we will mention touch to create the file.

Deleting a file in remote server-ansible file module

here we are deleting abcd.conf in the remote server. So at path parameter mention path of your file which you want to delete.

at path:  mention the path of the file in remote server.

at state:  mention absent to delete the file.

so at state parameter, we will use touch to create the file, absent to delete the e file.

creating a file with permissions-ansible file module

 

by using this module we can create the e file with permissions

at mode parameter we have total 4 digits. will mention starting will be always zero ramaining digits will be your file permissions.

at owner parameter, you can mention the  owner of the file

this permissions will be set to that newly created file

both the above codes work like same but here we are using symolic mode (equivalent to 0421).

Creating multiple new files-ansible file module

At path paremater we are using “{{ item }}” , by using this we can crete loop to create multiple files.

At with_items parameter, we have to mentions  file names which you want to create.

So by using “{{ item }}”  , with_items parameters we can crete loop and we can create multiple files.

Deleting multiple files-ansible file module

To create multiple files and to delete multiple files the code will be the same but small change in state parameter.

Touch to create files, absent to delete files.

you can refer in official ansible documentation https://docs.ansible.com/ansible/2.5/modules/file_module.html

[top]

Leave a Reply

Your email address will not be published. Required fields are marked *