FreeKB - fail module (skip tasks)
Ansible - fail module (skip tasks)

This assumes you have installed Ansible, are familiar with the ansible-playbook command and have created a playbook.

Let's say you have a playbook that performs two tasks, where the first task determines if the /tmp/dir1 directory exists and the second task determines if the /tmp/dir2 directory exists.

---
- hosts: all
  tasks:
    - name: determine if /tmp/dir1 exists
      stat:
        path: "/tmp/dir1"
      register: dir1

    - name: determine if /tmp/dir2 exists
      stat:
        path: "/tmp/dir2"
      register: dir2

 


Single target server

Running this playbook against a single target server should return the following output.

PLAY [all]

TASK [Gathering Facts]
ok: [server1.example.com]

TASK [determine if /tmp/dir1 exists]
ok: [server1.example.com]

TASK [determine if /tmp/dir2 exists]
ok: [server1.example.com]

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

 

fail can be used to cease execution of a playbook, like this.

---
- hosts: all
  tasks:
    - name: determine if /tmp/dir1 exists
      stat:
        path: "/tmp/dir1"
      register: dir1

    - fail: msg="fail when /tmp/dir1 does not exist"
      when: "dir1.stat.exists == false"

    - name: determine if /tmp/dir2 exists
      stat:
        path: "/tmp/dir2"
      register: dir2

    - fail: msg="fail when /tmp/dir2 does not exist"
      when: "dir2.stat.exists == false"

 

Running this playbook against a single target server should now return the following output. Notice that the task to determine if /tmp/dir2 exists was not played.

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

PLAY [all]

TASK [Gathering Facts]
ok: [server1.example.com]

TASK [determine if /tmp/dir1 exists]
ok: [server1.example.com]

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

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

 


Multiple target servers

Let's consider the scenario where there are two (or more) target servers. Let's say that /tmp/dir1 and /tmp/dir2 do not exist on server1.example.com and that the directories do 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 target server in question, and does not impact the other target servers.

PLAY [all]

TASK [Gathering Facts]
ok: [server1.example.com]
ok: [server2.example.com]

TASK [determine if /tmp/dir1 exists]
ok: [server1.example.com]
ok: [server2.example.com]

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

TASK [determine if /tmp/dir2 exists]
ok: [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 0ead5 in the box below so that we can be sure you are a human.




Comments