• Docs »
  • file - Sets attributes of files

file - Sets attributes of files


  • 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.


Parameter Choices/Defaults Comments
(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.

aliases: attr

(added in 1.8)
  • no
  • yes ←
This flag indicates that filesystem links, if they exist, should be followed.
Previous to Ansible 2.5, this was no by default.
  • no ←
  • yes
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).
Name of the group that should own the file/directory, as would be fed to chown.
Mode the file or directory should be. For those used to /usr/bin/chmod remember that modes are actually octal numbers. You must either specify the leading zero so that Ansible's YAML parser knows it is an octal number (like 0644 or 01777) or quote it (like '644' or '0644' so Ansible receives a string and can do its own conversion from string into number. Giving Ansible a number without following one of these rules will end up with a decimal number which will have unexpected results. As of version 1.8, the mode may be specified as a symbolic mode (for example, u+rwx or u=rw,g=r,o=r).
Name of the user that should own the file/directory, as would be fed to chown.
path to the file being managed. Aliases: dest, name

aliases: dest, name
  • no ←
  • yes
recursively set the specified file attributes (applies only to directories)
selevel Default:
Level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the range. _default feature works as for seuser.
Role part of SELinux file context, _default feature works as for seuser.
Type part of SELinux file context, _default feature works as for seuser.
User part of SELinux file context. Will default to system policy, if applicable. If set to _default, it will use the user portion of the policy if available.
path of the file to link to (applies only to state=link and state=hard). Will accept absolute, relative and nonexisting paths. Relative paths are relative to the file being created (path) which is how the UNIX command ln -s SRC DEST treats relative paths.
  • absent
  • directory
  • file ←
  • hard
  • link
  • touch
If directory, all intermediate subdirectories will be created if they do not exist. Since Ansible 1.7 they will be created with the supplied permissions. If file, the file will NOT be created if it does not exist; see the touch value or the copy or template module if you want that behavior. If link, the symbolic link will be created or changed. Use hard for hardlinks. If absent, directories will be recursively deleted, and files or symlinks will be unlinked. Note that absent will not cause file to fail if the path does not exist as the state did not change. If touch (new in 1.4), an empty file will be created if the path does not exist, while an existing file or directory will receive updated file access and modification times (similar to the way `touch` works from the command line).

(added in 2.2)
  • no ←
  • yes
Normally this module uses atomic operations to prevent data corruption or inconsistent reads from the target files, sometimes systems are configured or just broken in ways that prevent this. One example are docker mounted files, they cannot be updated atomically and can only be done in an unsafe manner.
This boolean option allows ansible to fall back to unsafe methods of updating files for those cases in which you do not have any other choice. Be aware that this is subject to race conditions and can lead to data corruption.




# change file ownership, group and mode. When specifying mode using octal numbers, first digit should always be 0.
- file:
    path: /etc/foo.conf
    owner: foo
    group: foo
    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
    - { 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"

# create a directory if it doesn't exist
- file:
    path: /etc/some_directory
    state: directory
    mode: 0755


This module is flagged as stableinterface which means that the maintainers for this module guarantee that no backward incompatible interface changes will be made.


This module is flagged as core which means that it is maintained by the Ansible Core Team. See Module Maintenance & Support for more info.

For a list of other modules that are also maintained by the Ansible Core Team, see here.


For more information about Red Hat’s support of this module, please refer to this Knowledge Base article


  • Ansible Core Team
  • Michael DeHaan


If you notice any issues in this documentation you can edit this document to improve it.