Reputation: 153
I have a simple test playbook
- name: Localhost stuff - Phase 1
hosts: localhost
gather_facts: no
tasks:
- debug:
msg: "Running Phase 1 on localhost"
- name: Remote tasks
hosts: all
gather_facts: no
ignore_unreachable: no
tasks:
- name: Run task on remote
shell: "ls -l"
- name: Localhost stuff - Phase 2
hosts: localhost
gather_facts: no
tasks:
- debug:
msg: "Running Phase 2 on localhost"
I don't understand why if the hosts: all section has two hosts where 1 is reachable and 1 not reachable, then the playbook will run to completion and run the second localhost task, whereas if there is only a single host, which is unreachable, then the second localhost task does not run and the playbook terminates after the remote command.
I assume ansible thinks it has no active hosts in the second case, but ignores the fact that localhost tasks exist.
How can I change the behaviour, so it does not terminate the entire playbook, but allows the localhost tasks to run?
Upvotes: 1
Views: 337
Reputation: 68189
Actually, ignore_unreachable
was designed to solve also this problem. As a result, it's up to you to handle unreachable hosts. For example, end the unreachable host(s) on your own
- name: Play1
hosts: localhost
gather_facts: false
tasks:
- ping:
- name: Play2
hosts: host03
gather_facts: false
tasks:
- name: End unreachable hosts
block:
- ping:
register: result
ignore_unreachable: true
- meta: end_host
when: result.unreachable|default(false)
- command: ls -l
- name: Play3
hosts: localhost
gather_facts: false
tasks:
- ping:
This playbook will proceed to the 3rd play also in case a single host will be unreachable in the 2nd play, e.g.
PLAY [Play1] *********************************************************************************
TASK [ping] **********************************************************************************
ok: [localhost]
PLAY [Play2] *********************************************************************************
TASK [ping] **********************************************************************************
fatal: [host03]: UNREACHABLE! => changed=false
msg: 'Failed to connect to the host via ssh: ssh: connect to host test_19 port 22: No route to host'
skip_reason: Host host03 is unreachable
unreachable: true
TASK [meta] **********************************************************************************
PLAY [Play3] *********************************************************************************
TASK [ping] **********************************************************************************
ok: [localhost]
PLAY RECAP ***********************************************************************************
host03 : ok=0 changed=0 unreachable=1 failed=0 skipped=1 rescued=0 ignored=0
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Upvotes: 0