'How to exit Ansible playbook when there are multiple hosts defined in the playbook

I have a sample Ansible playbook s.yml:

- hosts: localhost

  tasks:

    - debug: msg="{{ var }}"

    - debug: msg="Do a INFLATE"
      when: var == "inflate"

    - meta: end_play
      when: var == "inflate"

    - debug: msg="this is a DEFLATE"
      when: var == "deflate"

- hosts: bat

  tasks:

  - debug: msg="{{ hostname }}"

Basically, I want it to handle two conditions: inflate and deflate. If it is inflate, it will run some tasks and then exit(where the end_play is). If it is deflate, it will do something on a different hosts(bat in this sample which has wag-bat and dsg-bat).

When I run it for deflate:

$ ansible-playbook s.yml -e "var=deflate"

PLAY [localhost] ************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************
Sunday 22 May 2022  21:06:50 +0000 (0:00:00.022)       0:00:00.022 ************ 
ok: [localhost]

TASK [debug] ****************************************************************************************************************************************************************************************************************************
Sunday 22 May 2022  21:06:51 +0000 (0:00:01.032)       0:00:01.055 ************ 
ok: [localhost] => {
    "msg": "deflate"
}

TASK [debug] ****************************************************************************************************************************************************************************************************************************
Sunday 22 May 2022  21:06:51 +0000 (0:00:00.023)       0:00:01.078 ************ 
skipping: [localhost]

TASK [meta] *****************************************************************************************************************************************************************************************************************************
Sunday 22 May 2022  21:06:51 +0000 (0:00:00.021)       0:00:01.100 ************ 
skipping: [localhost]

TASK [debug] ****************************************************************************************************************************************************************************************************************************
Sunday 22 May 2022  21:06:51 +0000 (0:00:00.015)       0:00:01.116 ************ 
ok: [localhost] => {
    "msg": "this is a DEFLATE"
}

PLAY [bat] ******************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************
Sunday 22 May 2022  21:06:51 +0000 (0:00:00.040)       0:00:01.156 ************ 
ok: [wag-bat]
ok: [dsg-bat]

TASK [debug] ****************************************************************************************************************************************************************************************************************************
Sunday 22 May 2022  21:06:53 +0000 (0:00:01.549)       0:00:02.706 ************ 
ok: [wag-bat] => {
    "msg": "wag-bat"
}
ok: [dsg-bat] => {
    "msg": "dsg-bat"
}

It skipped the end_play and went on to bat and displayed the hostname which is defined in the /etc/ansible/host_vars/wag-bat and /etc/ansible/host_vars/dsg-bat files. This is what I want.

But when I run it for inflate:

$ ansible-playbook s.yml -e "var=inflate"

PLAY [localhost] ************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************
Sunday 22 May 2022  21:11:55 +0000 (0:00:00.021)       0:00:00.021 ************ 
ok: [localhost]

TASK [debug] ****************************************************************************************************************************************************************************************************************************
Sunday 22 May 2022  21:11:56 +0000 (0:00:01.017)       0:00:01.039 ************ 
ok: [localhost] => {
    "msg": "inflate"
}

TASK [debug] ****************************************************************************************************************************************************************************************************************************
Sunday 22 May 2022  21:11:56 +0000 (0:00:00.022)       0:00:01.062 ************ 
ok: [localhost] => {
    "msg": "Do a INFLATE"
}

TASK [meta] *****************************************************************************************************************************************************************************************************************************
Sunday 22 May 2022  21:11:56 +0000 (0:00:00.023)       0:00:01.085 ************ 

PLAY [bat] ******************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************
Sunday 22 May 2022  21:11:56 +0000 (0:00:00.018)       0:00:01.103 ************ 
ok: [dsg-bat]
ok: [wag-bat]

TASK [debug] ****************************************************************************************************************************************************************************************************************************
Sunday 22 May 2022  21:11:58 +0000 (0:00:02.131)       0:00:03.234 ************ 
ok: [wag-bat] => {
    "msg": "wag-bat"
}
ok: [dsg-bat] => {
    "msg": "dsg-bat"
}

It seems it did go through the end_play, however it did not exit the execution of the playbook but continued to get on to bat. If I remove the - hosts: bat portion from the playbook, it will stop at the end_play for inflate.

If there a way I can make it to stop at the end_play for inflate? I do need to have the - hosts: bat to handle the deflate part. I tried to use include_tasks to handle the deflate part, but I can not define the -hosts bat in the sub playbook.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source