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?