Skip to content

Instantly share code, notes, and snippets.

@evrardjp
Last active March 15, 2021 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save evrardjp/564ebf6c18d335c3f8d5eaad639870c1 to your computer and use it in GitHub Desktop.
Save evrardjp/564ebf6c18d335c3f8d5eaad639870c1 to your computer and use it in GitHub Desktop.
block/rescue removes host from ansible_play_hosts when block fails, even when rescued!
---
- hosts: all
connection: local
gather_facts: no
tasks:
- debug:
var: ansible_play_hosts
- block:
- fail:
msg: "Failure"
when:
- "inventory_hostname == 'localhost'"
rescue:
- debug:
msg: "Success"
always:
- debug:
var: ansible_play_hosts
@evrardjp
Copy link
Author

evrardjp commented Oct 13, 2017

ansible-playbook who-is-in-play.yml -i inventory

PLAY [all] *************************************************************************************************************************************************************************************************************************************************************************************************

TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "ansible_play_hosts": [
        "localhost",
        "localhost2",
        "aio",
        "localhost3"
    ],
    "changed": false
}
ok: [localhost2] => {
    "ansible_play_hosts": [
        "localhost",
        "localhost2",
        "aio",
        "localhost3"
    ],
    "changed": false
}
ok: [aio] => {
    "ansible_play_hosts": [
        "localhost",
        "localhost2",
        "aio",
        "localhost3"
    ],
    "changed": false
}
ok: [localhost3] => {
    "ansible_play_hosts": [
        "localhost",
        "localhost2",
        "aio",
        "localhost3"
    ],
    "changed": false
}

TASK [fail] ************************************************************************************************************************************************************************************************************************************************************************************************
skipping: [localhost2]
skipping: [aio]
skipping: [localhost3]
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Failure"}

TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "changed": false,
    "msg": "Success"
}

TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost2] => {
    "ansible_play_hosts": [
        "localhost2",
        "aio",
        "localhost3"
    ],
    "changed": false
}
ok: [localhost] => {
    "ansible_play_hosts": [
        "localhost2",
        "aio",
        "localhost3"
    ],
    "changed": false
}
ok: [aio] => {
    "ansible_play_hosts": [
        "localhost2",
        "aio",
        "localhost3"
    ],
    "changed": false
}
ok: [localhost3] => {
    "ansible_play_hosts": [
        "localhost2",
        "aio",
        "localhost3"
    ],
    "changed": false
}

PLAY RECAP *************************************************************************************************************************************************************************************************************************************************************************************************
aio                        : ok=2    changed=0    unreachable=0    failed=0
localhost                  : ok=3    changed=0    unreachable=0    failed=1
localhost2                 : ok=2    changed=0    unreachable=0    failed=0
localhost3                 : ok=2    changed=0    unreachable=0    failed=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment