file – Sets attributes of files
file – Sets attributes of files
Synopsis
- Sets attributes of files, symlinks, and directories, or removes files/symlinks/directories. Many other modules support the same options as the
file
module - including copy, template, and assemble. - For Windows targets, use the win_file module instead.
Parameters
Parameter | Choices/Defaults | Comments |
---|---|---|
access_time - added in 2.7 |
This parameter indicates the time the file's access time should be set to Should be Default is None meaning that | |
access_time_format - added in 2.7 |
Default: "%Y%m%d%H%M.%S" |
When used with Based on default Python format (see time.strftime doc) |
attributes - added in 2.3 |
Attributes the file or directory should have. To get supported flags look at the man page for chattr on the target system. This string should contain the attributes in the same order as the one displayed by lsattr.
| |
follow boolean added in 1.8 |
|
This flag indicates that filesystem links, if they exist, should be followed. Previous to Ansible 2.5, this was |
force boolean |
|
force the creation of the symlinks in two cases: the source file does not exist (but will appear later); the destination exists and is a file (so, we need to unlink the "path" file and create symlink to the "src" file in place of it). |
group - |
Name of the group that should own the file/directory, as would be fed to chown. | |
mode - |
Mode the file or directory should be. For those used to /usr/bin/chmod remember that modes are actually octal numbers. You must either add a leading zero so that Ansible's YAML parser knows it is an octal number (like | |
modification_time - added in 2.7 |
This parameter indicates the time the file's modification time should be set to Should be Default is None meaning that | |
modification_time_format - added in 2.7 |
Default: "%Y%m%d%H%M.%S" |
When used with Based on default Python format (see time.strftime doc) |
owner - |
Name of the user that should own the file/directory, as would be fed to chown. | |
path - / required |
Path to the file being managed.
| |
recurse boolean |
|
recursively set the specified file attributes (applies only to directories) |
selevel - |
Default: "s0" |
Level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the |
serole - |
Role part of SELinux file context, | |
setype - |
Type part of SELinux file context, | |
seuser - |
User part of SELinux file context. Will default to system policy, if applicable. If set to | |
src - |
path of the file to link to (applies only to | |
state - |
|
If |
unsafe_writes boolean added in 2.2 |
|
By default this module uses atomic operations to prevent data corruption or inconsistent reads from the target files, but sometimes systems are configured or just broken in ways that prevent this. One example is docker mounted files, which cannot be updated atomically from inside the container and can only be written in an unsafe manner. This option allows Ansible to fall back to unsafe methods of updating files when atomic operations fail (however, it doesn't force Ansible to perform unsafe writes). IMPORTANT! Unsafe writes are subject to race conditions and can lead to data corruption. |
Notes
Examples
# change file ownership, group and mode
- file:
path: /etc/foo.conf
owner: foo
group: foo
# when specifying mode using octal numbers, add a leading 0
mode: 0644
- file:
path: /work
owner: root
group: root
mode: 01777
- file:
src: /file/to/link/to
dest: /path/to/symlink
owner: foo
group: foo
state: link
- file:
src: '/tmp/{{ item.src }}'
dest: '{{ item.dest }}'
state: link
with_items:
- { src: 'x', dest: 'y' }
- { src: 'z', dest: 'k' }
# touch a file, using symbolic modes to set the permissions (equivalent to 0644)
- file:
path: /etc/foo.conf
state: touch
mode: "u=rw,g=r,o=r"
# touch the same file, but add/remove some permissions
- file:
path: /etc/foo.conf
state: touch
mode: "u+rw,g-wx,o-rwx"
# touch again the same file, but dont change times
# this makes the task idempotents
- file:
path: /etc/foo.conf
state: touch
mode: "u+rw,g-wx,o-rwx"
modification_time: "preserve"
access_time: "preserve"
# create a directory if it doesn't exist
- file:
path: /etc/some_directory
state: directory
mode: 0755
# updates modification and access time of given file
- file:
path: /etc/some_file
state: file
mode: 0755
modification_time: now
access_time: now
Status
- This module is guaranteed to have no backward incompatible interface changes going forward. [stableinterface]
- This module is maintained by the Ansible Core Team. [core]
Red Hat Support
More information about Red Hat’s support of this module is available from this Red Hat Knowledge Base article.
Authors
- Ansible Core Team
- Michael DeHaan
© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.7/modules/file_module.html