FreeKB - Ansible fail when file or directory does not exist
Ansible - fail when file or directory does not exist

Let's say you have a playbook that should only be executed when /opt/example.txt file exists. The stat module and the register parameter can be used to store the statistics of /opt/example.txt in a variable. In this example, the statistics of /opt/example.txt are stored in the variable named foo.

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

 

The debug module can be used to displays the statistics of example.txt.

- name: "output the 'foo' variable"
  debug: 
    msg: "{{ foo }}"

 

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

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

 

The fail module can now be used to cease execution of the playbook if /opt/example.txt does not exist.

- name: "fail when /opt/example.txt does not exist"
  fail: 
   msg: "/opt/example.txt does not exist"
  when: "foo.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 /opt/example.txt in the 'foo' variable]
ok: [server1.example.com]

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

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

 


Multiple managed nodes

Let's consider the scenario where there are two (or more) managed nodes. Let's say that /opt/example.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 /opt/example.txt in the 'foo' variable]
ok: [server1.example.com]
ok: [server2.example.com]

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

TASK [fail when /opt/example.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 c4a7e 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   |