Files
The Files
modules library includes modules allowing you to accomplish most tasks related to Linux file management, such as creating, copying, editing, and modifying permissions and other attributes of files.
Common files modules include blockinfile
,copy
,fetch
,file
,lineinfile
, stat
and synchronize
.
Use the file
module to touch
a file on managed hosts:
You can use the file
module to ensure that a new or existing file has the correct permissions or SELinux type:
The file module acts like chcon
when setting file contexts. After using file to set the context, use sefcontext
to update the SELinux policy
- name: Set SELinux type persistently
sefcontext:
target: /path/to/samba_file
setype: samba_share_t
state: present
Copying and Editing Files on Managed Hosts
The copy module is used to copy a file located in the Ansible working directory on the control node to selected managed hosts.
By default this module assumes that force: yes
is set. That forces the module to overwrite the remote file if it exists but contains different contents from the file being copied. If force: no
is set, then it only copies the file to the managed host if it does not already exist.
- To retrieve files from managed hosts use the
fetch
module. - To ensure a specific single line of text exists in an existing file, use the
lineinfile
module. - To add a block of text to an existing file, use the
blockinfile
module
Removing a File from Managed Hosts
To remove a file from managed hosts is to use the file module with the state: absent
.
The state parameter is optional to many modules. You should always make your intentions clear whether you want state: present
or state: absent
.
Retrieving the Status of a File
The stat
module retrieves facts for a file, similar to the Linux stat
command.
Synchronizing Files
The synchronize module is a wrapper around the rsync
tool.