Quit a playbook without error for a particular host

25 views
Skip to first unread message

isor...@gmail.com

unread,
Oct 20, 2022, 3:27:11 AM10/20/22
to Ansible Project
Hi, 

I have a playbook containing several Plays and each Play is executed on a bunch of hosts. 

I would like to quit the whole playbook (not only the current play) for a particular host if a  "when: condition" is met. 

Example Playbook with 2 Plays: 

- name: Play for patching precheck and download updates
  hosts: patchgroup
  remote_user: ansible
  gather_facts: true
  become: true
  timeout: 300
  roles:
    - patching_precheck_download

- name: Play for setting Icinga downtime
  hosts: patchgroup
  remote_user: ansible
  gather_facts: true
  roles:
    - role: icinga_downtime
      vars:
        host_and_services: true

In the role patching_precheck_download there is the following task

- block:
    - name: Red Hat | Quit playbook when no updates are found
      fail:
        msg: "Quit playbook for the current host since no updates are found."
    #- meta: end_host

  when: updates.stdout_lines | length == 0

If the when condition is met, I currently use "fail" which will end for the particular host, both the current play and the playbook. This is intended, but it will throw an error which should not be the case. 

The other alternative I found is "meta: end_host" this will not throw an error and end the play for the current host, but not for the whole Playbook (2nd Play will still be executed for this particular host). So, this is no solution. 

Any ideas how to quit a Play and Playbook without throwing an error ?

Many thanks
Rainer

Josh K

unread,
Oct 20, 2022, 3:40:07 PM10/20/22
to Ansible Project
Perhaps one solution is to set some fact (with set_fact) when this condition is met, like `finished: yes` and for subsequent plays/tasks you can add a line `when: finished is not defined`. Facts are set on a per-host basis, so I think this should work.

This isn't really quitting, but it achieves the same result by skipping those tasks. 

Reply all
Reply to author
Forward
0 new messages