I want to exit without an error (I know about assert and fail modules) when I meet a certain condition. The following code exits but with a failure:
tasks: - name: Check if there is something to upgrade shell: if apt-get --dry-run upgrade | grep -q "0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded"; then echo "no"; else echo "yes"; fi register: upgrading - name: Exit if nothing to upgrade fail: msg="Nothing to upgrade" when: upgrading.stdout == "no"
The default behavior is to pause with a prompt. You can use ctrl+c if you wish to advance a pause earlier than it is set to expire or if you need to abort a playbook run entirely. To continue early: press ctrl+c and then c . To abort a playbook: press ctrl+c and then a .
Ignoring failed commands By default Ansible stops executing tasks on a host when a task fails on that host. You can use ignore_errors to continue on in spite of the failure. The ignore_errors directive only works when the task is able to run and returns a value of 'failed'.
If you use ignore_errors, ansible will continue attempting to run tasks against that host.
If you assign the never tag to a task or play, Ansible will skip that task or play unless you specifically request it ( --tags never ).
Since Ansible 2.2, you can use end_play
with the meta module:
- meta: end_play
You can also specify when
for conditionally ending the play:
- meta: end_play when: upgrading.stdout == "no"
Note, though, that the task is not listed in the output of ansible-playbook, regardless of whether or not the play actually ends. Also, the task is not counted in the recap. So, you could do something like:
- block: - name: "end play if nothing to upgrade" debug: msg: "nothing to upgrade, ending play" - meta: end_play when: upgrading.stdout == "no"
which will announce the end of the play right before ending it, only when the condition is met. If the condition is not met, you'll see the task named end play if nothing to upgrade
appropriately skipped, which would provide more info to the user as to why the play is, or is not, ending.
Of course, this will only end the current play and not all remaining plays in the playbook.
UPDATE June 20 2019:
As reto mentions in comments, end_play
ends the play for all hosts. In Ansible 2.8, end_host
was added to meta:
end_host (added in Ansible 2.8) is a per-host variation of end_play. Causes the play to end for the current host without failing it.
UPDATE Feb 2021: fixed broken link to meta module
Just a little note: meta: end_play
ends just the play, not a playbook. So this playbook:
--- - name: 1st play with end play hosts: localhost connection: local gather_facts: no tasks: - name: I'll always be printed debug: msg: next task terminates first play - name: Ending the 1st play now meta: end_play - name: I want to be printed! debug: msg: However I'm unreachable so this message won't appear in the output - name: 2nd play hosts: localhost connection: local gather_facts: no tasks: - name: I will also be printed always debug: msg: "meta: end_play ended just the 1st play. This is 2nd one."
will produce this output:
$ ansible-playbook -i localhost, playbooks/end_play.yml PLAY [1st play with end play] ************************************************** TASK [I'll always be printed] ************************************************** ok: [localhost] => { "msg": "next task terminates first play" } PLAY [2nd play] **************************************************************** TASK [I will also be printed always] ******************************************* ok: [localhost] => { "msg": "meta: end_play ended just the 1st play. This is 2nd one." } PLAY RECAP ********************************************************************* localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With