How to check if a file/directory exists in Ansible

To get the details of a file or directory in a Linux system, we can use the Ansible stat module. It works similar to the Linux ‘stat’ command. Of course, the module provides much more detail than whether a file exists or not. We can know when the file was last modified, what all permissions the invoking user has about the file, whether it is a directory etc.

If you just need to check the status of the file to create a new file, then you should use the file module. It will create a file or directory only if the file does not exist. I will show you an example below. For more details on how to create directories in Ansible, you can refer this post.

The stat module needs the path of the file, directory or symlink to be checked at the minimum.

The stat module returns all the details regarding the object specified in the path parameter. We need to store this in a register. Then using the return values and the ‘when’ condition statements, we can check various properties of the files.

Checking if the file exists

Let’s see a primary example of how to check if an object, file/directory, exists using the stat module.
In the following example, I am checking the status of ‘prompt.yaml’ file. I am storing the details of the stat module execution in a register named ‘file_details’. The module returns a lot of return values, and one of them is ‘exists’.  It will return a boolean value, true or false, depending on the status of the file.

- hosts: all
  tasks:
  - name: Ansible check file exists example.
    stat:
      path: /Users/mdtutorials2/Documents/Ansible/prompt.yaml
    register: file_details

  - debug:
      msg: "The file or directory exists"
    when: file_details.stat.exists

The above example will give the output whether it is a file or directory or symlink. So how to check if a particular object exists and it is not a directory or symlink.

We can use the ‘isreg‘ return value for checking if its a return value. It will be true if the path is a regular file.

  - debug:
      msg: "Ansible file exists example"
    when: file_details.stat.exists and file_details.stat.isreg

Another way is to combine the ‘isdir‘ and ‘islnk‘ return values. The below task shows how it can be done.

  - debug:
      msg: "It is a file"
    when: file_details.stat.exists and file_details.stat.isdir == false and file_details.stat.islnk == false

Checking if a directory exists

For checking if a particular object is a directory and also it exists you can combine the ‘exists’ and ‘isdir’ return values. The ‘isdir’ value will be true if the object is a directory and else false.

For example, in the below case I have given the path for ‘test1’ object. If it is not a directory, the task will be skipped.

- hosts: all
  tasks:
  - name: Ansible check directory exists example.
    stat:
      path: /Users/dnpmacpro/Documents/Ansible/test1
    register: files_to_delete

  - debug:
      msg: "It is a directory"
    when: files_to_delete.stat.exists and files_to_delete.stat.isdir