How do I exit Ansible playbook without error on a condition -


i want exit without error (i know assert , fail modules) when meet condition. following code exits failure:

  tasks:      - name: check if there upgrade       shell: if apt-get --dry-run upgrade | grep -q "0 upgraded, 0 newly installed, 0 remove , 0 not upgraded"; echo "no"; else echo "yes"; fi       register: upgrading      - name: exit if nothing upgrade       fail: msg="nothing upgrade"       when: upgrading.stdout == "no" 

in ansible 2.2, can use end_play meta module:

- meta: end_play 

you can specify when conditionally ending play:

- meta: end_play   when: upgrading.stdout == "no" 

note, though, task not listed in output of ansible-playbook, regardless of whether or not play ends. also, task not counted in recap. so, like:

- block:     - name: "end play if nothing upgrade"       debug:         msg: "nothing upgrade, ending play"      - meta: end_play   when: upgrading.stdout == "no" 

which announce end of play right before ending it, when condition met. if condition not met, you'll see task named end play if nothing upgrade appropriately skipped, provide more info user why play is, or not, ending.

of course, end current play , not all remaining plays in playbook.


Comments