Home Devops Checking if a file exists in Ansible

Checking if a file exists in Ansible

by The Linux Digest Guy
Ansible logo

There can be a slew of reasons you might want to check if a file exists in Ansible. Possibly, you want to create it if it doesn’t. But did you know you can perform a totally unrelated action based on the existence of a specific file or directory?

To check if a file exists in Ansible, you can use the built-in stat module. Stat will look at the file and get its properties and state. You can then check if the file exists by looking at the resulting variable.

I have compiled a step by step guide below to get an understanding of how the stat module works. Keep reading to get the full experience.

Get the state of the file

First of all we need to create a playbook to test the functionality. Below I have the beginning of the playbook. Please replace the host and user variables to fit your environment.


- name: check if myfile.txt exists
  hosts: testserver.linuxdigest.com
  user: root
    - name: Get stats about myfile.txt
        path: /etc/myfile.txt
      register: myvar
    - debug:
        msg: "The contents of myvar: {{ myvar }}"

As you can see, we are checking for a file at the path /etc/myfile.txt. We tell Ansible to put the results of stat into a variable called myvar. After getting the information we want, we print what the variable now contains. Here is the output we get if the file exists:

Wow, that is a lot of information! Now we can see the permissions of the file, if it is a symlink when it was created, modified and a bunch of other stuff we don’t need right now. But, obviously, we can use the stat module to check for much more than just if the file exists.

But in this example, we only care about one value:

'stat': {'exists': True.....

We now know that the file exists. So now we just need to act on that information.

Perform action if the file exists

In the output above, the exists variable was within a dictionary called “stat”. So we can access the result with this path:


The value of this variable will be “True” if the file exists and “False” if it does not. We can now use the “when” conditional keyword with the action we want to take.

Now, let’s add an action. Just to test this premise, we will use the debug module to print a message if this file exists.

    - debug:
        msg: "The file myfile.txt exists!"
      when: myvar.stat.exists
    - debug:
        msg: "The file myfile.txt does not exists"
      when: myvar.stat.exists == False

This will print the correct line out, depending on if the file exists or not. Here is the output when the file exists:

We can see from the output, that the file exists. The second debug message is just skipped because it does not match.

Of course, we can perform any action that we want. Not just print the result. As an example, we could install some piece of software if the file exists but uninstall it if the file does not exist.

If for some reason you wanted to make sure Apache is installed if the file exists, you could do something like this:

    - apt:
        name: apache2
        state: present
      when: myvar.stat.exists == True
    - apt:
        name: apache2
        state: absent
      when: myvar.stat.exists == False

Check if it is a file or directory

As we saw in the output of the stat module, we can check for other things than just the existence of the file.

It might be important to know if we are working with a file or a directory. For this we can check for the “isdir” variable. If the path is not a directory, the “isdir” variable might be undefined. So we need to verify if it exists before checking the value. Otherwise, we might get an error.

    - debug:
        msg: "myfile.txt is a directory!"
      when: myvar.stat.isdir is defined and myvar.stat.isdir

The simple way to create a file if it does not exist.

Just as a side note, you can also make sure a file exists with one command. I only include this in case somebody is using the stat module to create a file if it does not exist. Using the touch state in the file module. This way, the file will be created if it does not exist. Please note that if the file already exists it will be updated with a new modification date.

To skip updating the modification time and leave the file as is if it exists, you can add “modification_time: preserve”

- name: Touch a file. Creates the file if it does not exist. Otherwise, do nothing
    path: /etc/myfile.txt
    state: touch
    owner: root
    mode: 0644
    modification_time: preserve
    access_time: preserve

That’s it for now. I hope this helps. I will be writing more ansible tutorials in the future. In the mean time you can check out How to access hostvars in Ansible.


Related Posts

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy