ansible.builtin.file (v2.4.3.0-1) — module

Sets attributes of files

| "added in version" historical of ansible.builtin"

Authors: Ansible Core Team, Michael DeHaan

stableinterface | supported by core

Install Ansible via pip

Install with pip install ansible==2.4.3.0.post1

Description

Sets attributes of files, symlinks, and directories, or removes files/symlinks/directories. Many other modules support the same options as the C(file) module - including M(copy), M(template), and M(assemble).

For Windows targets, use the M(win_file) module instead.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- file:
    src: /file/to/link/to
    dest: /path/to/symlink
    owner: foo
    group: foo
    state: link
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- file:
    src: '/tmp/{{ item.src }}'
    dest: '{{ item.dest }}'
    state: link
  with_items:
    - { src: 'x', dest: 'y' }
    - { src: 'z', dest: 'k' }
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# 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"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# touch the same file, but add/remove some permissions
- file:
    path: /etc/foo.conf
    state: touch
    mode: "u+rw,g-wx,o-rwx"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# create a directory if it doesn't exist
- file:
    path: /etc/some_directory
    state: directory
    mode: 0755

Inputs

    
src:
    default: null
    description:
    - path of the file to link to (applies only to C(state=link)). Will accept absolute,
      relative and nonexisting paths. Relative paths are not expanded.
    required: false

mode:
    description:
    - The permissions the resulting filesystem object should have.
    - For those used to I(/usr/bin/chmod) remember that modes are actually octal numbers.
      You must give Ansible enough information to parse them correctly. For consistent
      results, quote octal numbers (for example, V('644') or V('1777')) so Ansible receives
      a string and can do its own conversion from string into number. Adding a leading
      zero (for example, V(0755)) works sometimes, but can fail in loops and some other
      circumstances.
    - Giving Ansible a number without following either of these rules will end up with
      a decimal number which will have unexpected results.
    - As of Ansible 1.8, the mode may be specified as a symbolic mode (for example, V(u+rwx)
      or V(u=rw,g=r,o=r)).
    - If O(mode) is not specified and the destination filesystem object B(does not) exist,
      the default C(umask) on the system will be used when setting the mode for the newly
      created filesystem object.
    - If O(mode) is not specified and the destination filesystem object B(does) exist,
      the mode of the existing filesystem object will be used.
    - Specifying O(mode) is the best way to ensure filesystem objects are created with
      the correct permissions. See CVE-2020-1736 for further details.
    type: raw

path:
    aliases:
    - dest
    - name
    default: []
    description:
    - 'path to the file being managed.  Aliases: I(dest), I(name)'
    required: true

force:
    choices:
    - 'yes'
    - 'no'
    default: 'no'
    description:
    - '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).'
    required: false

group:
    description:
    - Name of the group that should own the filesystem object, as would be fed to I(chown).
    - When left unspecified, it uses the current group of the current user unless you
      are root, in which case it can preserve the previous ownership.
    type: str

owner:
    description:
    - Name of the user that should own the filesystem object, as would be fed to I(chown).
    - When left unspecified, it uses the current user unless you are root, in which case
      it can preserve the previous ownership.
    - Specifying a numeric username will be assumed to be a user ID and not a username.
      Avoid numeric usernames to avoid this confusion.
    type: str

state:
    choices:
    - file
    - link
    - directory
    - hard
    - touch
    - absent
    default: file
    description:
    - If C(directory), all immediate subdirectories will be created if they do not exist,
      since 1.7 they will be created with the supplied permissions. If C(file), the file
      will NOT be created if it does not exist, see the M(copy) or M(template) module
      if you want that behavior.  If C(link), the symbolic link will be created or changed.
      Use C(hard) for hardlinks. If C(absent), directories will be recursively deleted,
      and files or symlinks will be unlinked. Note that C(absent) will not cause C(file)
      to fail if the C(path) does not exist as the state did not change. If C(touch) (new
      in 1.4), an empty file will be created if the C(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).
    required: false

follow:
    choices:
    - 'yes'
    - 'no'
    default: 'no'
    description:
    - This flag indicates that filesystem links, if they exist, should be followed.
    required: false
    version_added: '1.8'
    version_added_collection: ansible.builtin

serole:
    description:
    - The role part of the SELinux filesystem object context.
    - When set to V(_default), it will use the C(role) portion of the policy if available.
    type: str

setype:
    description:
    - The type part of the SELinux filesystem object context.
    - When set to V(_default), it will use the C(type) portion of the policy if available.
    type: str

seuser:
    description:
    - The user part of the SELinux filesystem object context.
    - By default it uses the V(system) policy, where applicable.
    - When set to V(_default), it will use the C(user) portion of the policy if available.
    type: str

recurse:
    choices:
    - 'yes'
    - 'no'
    default: 'no'
    description:
    - recursively set the specified file attributes (applies only to state=directory)
    required: false
    version_added: '1.1'
    version_added_collection: ansible.builtin

selevel:
    description:
    - The level part of the SELinux filesystem object context.
    - This is the MLS/MCS attribute, sometimes known as the C(range).
    - When set to V(_default), it will use the C(level) portion of the policy if available.
    type: str

attributes:
    aliases:
    - attr
    description:
    - The attributes the resulting filesystem object should have.
    - To get supported flags look at the man page for I(chattr) on the target system.
    - This string should contain the attributes in the same order as the one displayed
      by I(lsattr).
    - The C(=) operator is assumed as default, otherwise C(+) or C(-) operators need to
      be included in the string.
    type: str
    version_added: '2.3'
    version_added_collection: ansible.builtin

unsafe_writes:
    default: false
    description:
    - Influence when to use atomic operation to prevent data corruption or inconsistent
      reads from the target filesystem object.
    - By default this module uses atomic operations to prevent data corruption or inconsistent
      reads from the target filesystem objects, but sometimes systems are configured or
      just broken in ways that prevent this. One example is docker mounted filesystem
      objects, 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 filesystem
      objects 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.
    type: bool
    version_added: '2.2'
    version_added_collection: ansible.builtin