Bootstrap FreeKB - Ansible - End play using the fail module
Ansible - End play using the fail module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

There are a number of ways to do something when a task returns an error.

The fail module is used to end a play for a host when a certain condition is met. In this example, the playbook will fail when the foo variable is undefined.

---
- hosts: localhost
  tasks:
  - name: fail when the 'foo' variable is undefined
    fail: 
      msg: the 'foo' variable is undefined
    when: foo is undefined
...

 

It's important to recognize that when the when condition in the fail module evaluates to true:

  • The play will be ended for the host meaning that the subsequent tasks in the playbook will not be executed on the host
  • The task to return fatal and the PLAY RECAP with have failed=1.

meta: end_host can be used if you want the PLAY RECAP to have "skipped" instead of "failed".

TASK [fail when the 'foo' variable is undefined]
fatal: [server1.example.com]: FAILED! => {"changed": false, "msg": "The 'foo' variable is undefined"}

PLAY RECAP *********************************************************************
server1.example.com    : ok=0   changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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