Abort execution of remaining task if certain condition is failed
You can use
- assert https://docs.ansible.com/ansible/latest/collections/ansible/builtin/assert_module.html
- or fail https://docs.ansible.com/ansible/latest/collections/ansible/builtin/fail_module.html
It will go along with something like this
#check if params are invalid then abort below all tasks.
- name: 'check parm is null or invalid'
fail: msg="Please enter correct Params"
when: "param1 is not defined or param2 is not defined " ## whatever condition you want
So in Ansible 2.2+ there is the meta module: http://docs.ansible.com/ansible/latest/meta_module.html
So
meta: end_play
stops the playbook with a non failing status
Ansible >= 2.0 has a block feature that allows you to logically group tasks. This allows you to apply a when to a group of tasks.
The main difference between this and the fail or assert modules is that the task isn't marked as failed; it's just skipped. Whether this is better depends on your use case. For example I have some tasks that write to a log that's later parsed for failures; it's easier to do this if only "real" failure conditions are logged.
Example code:
- block:
# task 1
# task 2
# task 3
when: "param1 is defined or param2 is defined"
# otherwise output a message
- block:
debug: msg="Missing params"
when: "param1 is not defined or param2 is not defined"