Ansible - when file or directory exists or does not exist

The stat module or find module can be used to determine if a file or directory exists on a managed node or on your Ansible control node. If you need to determine if a file exists on an HTTP web server, the uri module can be used.


Stat Module

For example, let's say you have a task that should only be executed when /tmp/foo.txt file exists. The stat module and the register parameter can be used to store the statistics of /tmp/foo.txt in a variable.

---
- hosts: all
  tasks:
    - name: store the statistics of /tmp/foo.txt in the 'out' variable
      stat:
        path: /tmp/foo.txt
      register: out

 

The debug module can be used to displays the out variable.

- name: display the 'out' variable
  debug: 
    var: out

 

Which should return something like this. Notice in this example the "exists" is false.

TASK [display the 'out' variable] 
ok: [server1.example.com] => {
    "msg": {
        "changed": false, 
        "failed": false, 
        "stat": {
            "exists": false
        }
    }
}

 

The fail module and when parameter with out.stat.exists can now be used to fail if /tmp/foo.txt does not exist.

- name: fail when /tmp/foo.txt does not exist
  fail: 
   msg: /tmp/foo.txt does not exist
  when: out.stat.exists == false

 

Invoking this playbook should return something like this. The ansible-doc fail command can be used to show the Ansible documention on the fail module.

When fail evaluates to true, all of the subsequent tasks are skipped.

PLAY [all]

TASK [store the statistics of /tmp/foo.txt in the 'out' variable]
ok: [server1.example.com]

TASK [fail when /tmp/foo.txt does not exist]
fatal: [server1.example.com]: FAILED => {"changed": false, "msg": "/tmp/foo.txt does not exist"}

PLAY RECAP
server1.example.com : ok=2  changed=0  unreacable=0  failed=1

 


Find Module

Similarly, the find module and the register parameter can be used to store the statistics of /tmp/foo.txt in a variable.

---
- hosts: all
  tasks:
    - name: store the statistics of /tmp/foo.txt in the 'out' variable
      stat:
        path: /tmp/foo.txt
      register: out

 

The debug module can be used to displays the out variable.

- name: display the 'out' variable
  debug: 
    var: out

 

Which should return something like this. Notice in this example the "matched" is 0.

TASK [debug] 
ok: [server1.example.com] => {
    "msg": {
        "changed": false, 
        "examined": 0, 
        "failed": false, 
        "files": [], 
        "matched": 0, 
        "msg": "/tmp/foo.txt was skipped as it does not seem to be a valid directory or it cannot be accessed\n"
    }
}

 

The fail module and when parameter with out.mached can now be used to fail if /tmp/foo.txt does not exist.

- name: fail when /tmp/foo.txt does not exist
  fail: 
   msg: /tmp/foo.txt does not exist
  when: out.matched == 0

 

 


Multiple managed nodes

Let's consider the scenario where there are two (or more) managed nodes. Let's say that /tmp/foo.txt does not exist on server1.example.com but does exist on server2.example.com. In this scenario, invoking the playbook will return the following.

The important takeaway here is that when fail evaluated to true for server1.example.com, all of the subsequent tasks are skipped for only server1.example.com. All of the tasks were performed on server2.example.com. When fail evaluates to true, the fail is isolated to the managed node in question, and does not impact the other managed nodes.

PLAY [all]

TASK [store the statistics of /tmp/foo.txt in the 'out' variable]
ok: [server1.example.com]
ok: [server2.example.com]

TASK [fail when /tmp/foo.txt does not exist]
fatal: [server1.example.com]: FAILED => {"changed": false, "msg": "/tmp/foo.txt does not exist"}

TASK [fail when /tmp/foo.txt does not exist]
skipping: [server2.example.com]

PLAY RECAP
server1.example.com : ok=2  changed=0  unreacable=0  failed=1
server2.example.com : ok=3  changed=0  unreacable=0  failed=0

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter c8e09 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |