If I pass ignore_unreachable: true
, then it exits with 0
but reachable
also contains the unreachable host(s), which is what I don't want to.
With a minimal example playbook
---
- hosts: all
gather_facts: true
ignore_unreachable: true
tasks:
- name: Get the first good host in the group
set_fact:
first_good_host: "{{ ansible_play_hosts | map('extract', hostvars) | list | json_query(query) | first }}"
vars:
query: "[?ansible_facts!=''].inventory_hostname"
delegate_to: localhost
run_once: yes
- name: Aggregate the first good host to reachable group
ansible.builtin.add_host:
name: '{{ first_good_host }}'
groups: 'reachable'
- hosts: "{{ groups['reachable'] }}"
tasks:
- name: Print reachable hosts
debug:
msg: "{{ ansible_play_hosts }}"
I am not able reproduce that
... also contains the unreachable host(s) ...
The dynamic group reachable
will only contain the first reachable host resulting into an output of
PLAY [all] *****************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************
ok: [test2.example.com]
ok: [test1.example.com]
fatal: [test3.example.com]: UNREACHABLE! => changed=false
msg: 'Failed to connect to the host via ssh: ssh: connect to host test3.example.com port 22: No route to host'
skip_reason: Host test3.example.com is unreachable
unreachable: true
TASK [Get the first good host in the group] ********************************************************************
ok: [test1.example.com -> localhost]
TASK [Aggregate the first good host to reachable group] ********************************************************
changed: [test1.example.com]
PLAY [[u'test1.example.com']] **********************************************************************************
TASK [Gathering Facts] *****************************************************************************************
ok: [test1.example.com]
TASK [Print reachable hosts] ***********************************************************************************
ok: [test1.example.com] =>
msg:
- test1.example.com
PLAY RECAP *****************************************************************************************************
test1.example.com: ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test2.example.com: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test3.example.com: ok=0 changed=0 unreachable=1 failed=0 skipped=1 rescued=0 ignored=0
and if called via
ansible-playbook unreachable.yml; echo $?
0
Removing the line
ignore_unreachable: true
would result into
ansible-playbook unreachable.yml; echo $?
4
Please take note of the other answer here and Playbook execution.
But this playbooks exists with 4
if there is any unreachable host(s). (annot.: ... and without using ignore_unreachable: true
)
Right, that is the expected behavior.
How can I deal (annot.: avoid?) with exit code 4
without using ignore_unreachable: true
?
You can't avoid it since that Return / Exit Code is provided from the ansible-playbook
command itself at the very end of the run, not from within the playbook and as far as I understand. Usually I am not linking external content, but in this case you may have a look into Ansible Exit Codes.
Furthermore you might also take advantage from What is the XY problem.