Ansible File Module: Manage Files and Directories on Remote Nodes

The file module in Ansible is a versatile and essential tool for managing files and directories across remote hosts. This Ansible module enables you to create, delete, modify permissions, and manage file ownership on the remote nodes.

The file module can be used for:

  • Ensuring a file or directory is present or absent.
  • Changing file permissions and ownership.
  • Creating symbolic and hard links.
  • Touching a file (updating its modification time).

In this article, we will go through the real-world use cases of the file module with practical examples.

Basic syntax of Ansible File Module

Before diving into the examples, it’s important to understand the syntax of the file module. Here's a general overview:

- name: Manage file or directory
  ansible.builtin.file:
    path: /path/to/file_or_directory
    state: [absent|directory|file|touch|link|hard|mounted]
    mode: '0644'
    owner: username
    group: groupname

Parameters:

  • path: This specifies the location of the file or directory that you want to manage.
  • state: Defines the desired state of the file or directory. The most common values are:
  • mode: Defines the permissions for the file or directory (e.g., 0755 for directories).
  • owner and group: Define the user and group that own the file or directory.

Creating a directory using Ansible File Module

Creating directories with the correct permissions is a common task in Ansible playbooks. To create a directory using the file module, you can use the state: directory option.

Here’s an example playbook to create a directory at /tmp/new_directory:

- name: Create a directory
  ansible.builtin.file:
    path: /tmp/new_directory
    state: directory
    mode: '0755'

Here is a brief explanation of each parameter:

  • path: Specifies the location where the directory should be created.
  • state: directory: Ensures that the path will be a directory.
  • mode: Sets the permissions of the directory to 0755, allowing read, write, and execute permissions for the owner, and read and execute permissions for the group and others.

Changing permissions of a file

Modifying file permissions is another common task. You can use the file module to change the permissions of an existing file by specifying the mode parameter.

- name: Change file permissions
  ansible.builtin.file:
    path: /tmp/sample_file.txt
    mode: '0644'

Here is the explanation:

  • path: Specifies the file whose permissions will be modified.
  • mode: Changes the file’s permissions to 0644, which allows the owner to read and write, while others can only read.

Removing files and directories

Sometimes you may need to remove files or directories as part of your automation process. The file module makes this easy by using the state: absent option.

Example 1: Removing a file

- name: Remove a file
  ansible.builtin.file:
    path: /tmp/sample_file.txt
    state: absent

Here is a breakdown for above playbook:

  • path: Specifies the location of the file to be removed.
  • state: absent: Ensures the file is deleted if it exists.

Example 2: Removing a directory

- name: Remove a directory
  ansible.builtin.file:
    path: /tmp/new_directory
    state: absent

A brief explanation of the above playbook is shown below:

  • path: Specifies the location of the directory to be removed.
  • state: absent: Ensures the directory is deleted if it exists.

Modifying File Ownership

File ownership is another important aspect of managing files in a multi-user environment. You can use the file module to change the ownership of files and directories by specifying the owner and group parameters.

- name: Change file ownership
  ansible.builtin.file:
    path: /tmp/sample_file.txt
    owner: new_user
    group: new_group

Let's breakdown each parameter:

  • path: Specifies the location of the file whose ownership you want to modify.
  • owner: Changes the file’s owner to new_user.
  • group: Changes the file’s group to new_group.

Symbolic links (symlinks) are a convenient way to create references to files or directories. You can create symlinks using the file module by specifying state: link.

- name: Create a symbolic link
  ansible.builtin.file:
    src: /tmp/sample_file.txt
    dest: /tmp/sample_symlink.txt
    state: link

Here is the explanation of above playbook:

  • src: The source file to which the symlink will point.
  • dest: The location where the symlink will be created.
  • state: link: Specifies that a symbolic link should be created.
✍️
Author: Hitesh Jethwa has more than 15+ years of experience with Linux system administration and DevOps. He likes to explain complicated topics in easy to understand way.