Updated April 6, 2023
Introduction to Ansible File Module
In Ansible, we have many modules to work with files, directories, and links on remote target nodes like a copy, template, fetch, file etc. which have different features. In this article, we will learn about file module, which is mainly used to deal with files, directories, and symlinks. This module is used to manage properties and set or remove attributes of files, directories, and symlinks. For windows systems, Ansible provides a similar module named win_file. Which mostly work the same way with limitations of Window operating systems.
Explaining the Ansible File Module
Ansible file module takes the parameters and options mentioned by you in playbooks. Then these are sent to target remote nodes where the tasks are parsed into command set and executed accordingly.
In this module’s parameter, you must note that all the execution will be done on remote target nodes, so when changing ownership of files, directories; then relevant user and group must exist on remote target nodes, else playbook execution will fail. So in such kind of cases, its always better to check user or group’s existence first on remote target nodes, then try to set ownership to those users or groups.
How Does Ansible File Module Works?
Ansible file module have a set of pre-defined parameters and related acceptable/default values. Using the combination of these parameters you can do almost all the management of files, directories, or sysmlinks. Below we have mentioned some of the important parameters, if not all.
- access_time: This parameter is used to set the file’s access Default is “preserve” means no modification needed for files, directories, soft links, and hard links. Also for new files where state is touch, then default is “now”.
- access_time_format: This is parameter is used when we are also using access_time This parameter is used to set the time format of access time of files. Default format is based on the default python-format on your remote machine. But mostly it is “%Y%m%d%H%M.%S”.
- attributes: To set the attributes of resulting directory or The acceptable flags are same as chattr. Which can be seen by using lsattr.
- follow: This is to set whether filesystems links should be followed or not. Default is yes. Acceptable values are yes and no.
- force: This is to force the creation of syslinks. Acceptable values are yes and no. default is yes.
- group: – This is used to set the group ownership of a file or diretory.
- mode: – To set the permission of target file or directory. Better practice is use 4 octal numbers inside single quotes to represent the permission like ‘0777’ or ‘0644’.
- modification_time: – To set the file’s modification time. Default is “preserve” means no modification needed for files, directories, soft links, and hard links. Also for new files where state is touch, then default is “now”.
- modification_time_format: – This is parameter is used when we are also using modification_time This parameter is used to set the format of modification time of files. Default format is based on the default python format on your remote machine. But mostly it is “%Y%m%d%H%M.%S”.
- owner: – To Set the owner of file or directory.
- path: – The file’s path, which is our task’s target.
- recurse: – This is used when state parameter have directory as value and we want to update the content of a directory in terms of file attributes.
- selevel, serole, setype, seuser: – These are used to update the selinux file context.
- src: – This is to give the path of the file to link to.
- state: – Acceptable values are touch, absent, directory, file, hard and link. Default value is file.
We will explore some of these parameters in next section with some examples.
Examples
Now by using examples, we will try to learn about Ansible file module, which you might have to use in day to day operations. We will take some examples, but before going there, we first understand our lab, we used for testing purpose. Here we have an Ansible control server named ansible-controller and two remotes hosts named host-one and host-two. We will create playbooks and run Ansible commands on ansible-controller node and see the results on remote hosts.
- In this example, we have a playbook like below, using which we try to change the permissions and ownership of a file on remote target nodes.
hosts: all tasks:
- name: Here we check the current status of file BEFORE changing command: ls -l /tmp/sample.cfg
register: output_before
- debug:
var: output_before[‘stdout_lines’]
- name: Here we change the permissions and ownership of a file on all remote file:
path: /tmp/sample.cfg owner: testuser
group: testgroup mode: ‘0755’
- name: Here we check the current status of file AFTER changing command: ls -l /tmp/sample.cfg
register: output_after
- debug:
var: output_after[‘stdout_lines’]
Then run the playbook like below: –
ansible-playbook ansible_file.yaml
We get the output like below, where we can see what was file’s ownership before and what is after the changing using file module.
- In this example, we have a playbook like below, using which we try to make one link each for two files
—
hosts: all tasks:
- name: here we will create hard links file:
src: /var/tmp/{{ item.src }} path: /tmp/{{ item.dest }} state: link
loop:
- { src: sample.ini, dest: ini_file }
- { src: sample.cfg, dest: cfg_file }
- name: Here we print the ls -l for the links got created shell: ls -l /tmp/*_file
register: output_var
- debug:
var: output_var[‘stdout_lines’]
We get the output like below: –
ansible-playbook ansible_file_link.yaml
Conclusion
As we saw in this article, file module is easy to use and capable of managing files, directories and symlinks on remote target nodes. This module works based on state of a concerned file or directory. Also note that many of the other modules of same file-based type as copy, template uses the same options as file module. So learn them first and then use them.
Recommended Articles
This is a guide to Ansible File Module. Here we discuss the Introduction, how does Ansible File Module works? and examples. You may also have a look at the following articles to learn more –