classic solution to detect unreachable host in ansible does not work for a single host

51 views Asked by At

My ansible inventory has 3 hosts one of which is unreachable i.e remhost2.

Below is desired working playbook along with the desired output.

[wladmin@remhost1 ~]$ cat inventory.hosts

[remhosts]
remhost1
remhost2
remhost3

[wladmin@remhost1 ~]$ cat getur.yml

---
- name: Check unreachable hosts
  hosts: "remhosts"
  gather_facts: false
  any_errors_fatal: false
  tasks:
    - name: Print group names
      debug:
        msg: "GROUP NAME TO BE TESTED: {{ group_names }}"
      run_once: true

    - name: Perform ping in Template
      ping:

    - block:
        - debug:
            var: ansible_play_hosts_all
        - debug:
            var: ansible_play_hosts
        - set_fact:
            down: "{{ ansible_play_hosts_all|difference(ansible_play_hosts) }}"
        - debug:
            var: down
      run_once: true

    - name: "Display unreachable hosts one at a time"
      debug:
        msg: "Unreachable Host: {{ item }}"
      loop: "{{ ansible_play_hosts_all | difference(ansible_play_hosts) }}"
      run_once: true

    - fail:
        msg: "Exiting play if target hosts are unreachable"
      when: ansible_play_hosts_all != ansible_play_hosts

Run and desired Output:

[wladmin@remhost1 ~]$ ansible-playbook -i inventory.hosts getur.yml

[DEPRECATION WARNING]: [defaults]callback_whitelist option, normalizing names to new standard, use callbacks_enabled instead. This feature will be removed from ansible-
core in version 2.15. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[WARNING]: ansible.utils.display.initialize_locale has not been called, this may result in incorrectly calculated text widths that can cause Display to print incorrect
line lengths

PLAY [Check unreachable hosts] *********************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************
Wednesday 13 March 2024  14:12:15 -0500 (0:00:00.010)       0:00:00.010 *******
[WARNING]: Platform linux on host remhost1 is using the discovered Python interpreter at /usr/local/bin/python3.8, but future installation of another Python interpreter
could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.12/reference_appendices/interpreter_discovery.html for more information.
ok: [remhost1]
[WARNING]: Platform linux on host remhost3 is using the discovered Python interpreter at /usr/local/bin/python3.8, but future installation of another Python interpreter
could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.12/reference_appendices/interpreter_discovery.html for more information.
ok: [remhost3]
fatal: [remhost2]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: \nThis system is for the use by authorized users only. All data contained\non all systems is owned by the company and may be monitored, intercepted,\nrecorded, read, copied, or captured in any manner and disclosed in any\nmanner, by authorized company personnel. Users (authorized or unauthorized)\nhave no explicit or implicit expectation of privacy. Unauthorized or improper\nuse of this system may result in administrative, disciplinary action, civil\nand criminal penalties. Use of this system by any user, authorized or\nunauthorized, constitutes express consent to this monitoring, interception,\nrecording, reading, copying, or capturing and disclosure.\n\nIF YOU DO NOT CONSENT, LOG OFF NOW.\n\n##################################################################\n# *** This Server is using Centrify                          *** #\n# *** Remember to use your Active Directory account          *** #\n# ***    password when logging in                            *** #\n##################################################################\n\nwladmin@remhost2: Permission denied (publickey,password,keyboard-interactive).", "unreachable": true}

TASK [Print group names] ***************************************************************************************************************************************************
Wednesday 13 March 2024  14:12:19 -0500 (0:00:04.463)       0:00:04.473 *******
ok: [remhost1] => {
    "msg": "GROUP NAME TO BE TESTED: ['remhosts']"
}

TASK [Perform ping in Template] ********************************************************************************************************************************************
Wednesday 13 March 2024  14:12:19 -0500 (0:00:00.022)       0:00:04.496 *******
ok: [remhost1]
ok: [remhost3]

TASK [debug] ***************************************************************************************************************************************************************
Wednesday 13 March 2024  14:12:20 -0500 (0:00:00.448)       0:00:04.944 *******
ok: [remhost1] => {
    "ansible_play_hosts_all": [
        "remhost1",
        "remhost2",
        "remhost3"
    ]
}

TASK [debug] ***************************************************************************************************************************************************************
Wednesday 13 March 2024  14:12:20 -0500 (0:00:00.023)       0:00:04.967 *******
ok: [remhost1] => {
    "ansible_play_hosts": [
        "remhost1",
        "remhost3"
    ]
}

TASK [set_fact] ************************************************************************************************************************************************************
Wednesday 13 March 2024  14:12:20 -0500 (0:00:00.024)       0:00:04.992 *******
ok: [remhost1]

TASK [debug] ***************************************************************************************************************************************************************
Wednesday 13 March 2024  14:12:20 -0500 (0:00:00.040)       0:00:05.032 *******
ok: [remhost1] => {
    "down": [
        "remhost2"
    ]
}

TASK [Display unreachable hosts one at a time] ***********************************************************************************************************
Wednesday 13 March 2024  14:12:20 -0500 (0:00:00.041)       0:00:05.074 *******
ok: [remhost1] => (item=remhost2) => {
    "msg": "Unreachable Host: remhost2"
}

TASK [fail] ****************************************************************************************************************************************************************
Wednesday 13 March 2024  14:12:20 -0500 (0:00:00.032)       0:00:05.106 *******
fatal: [remhost1]: FAILED! => {"changed": false, "msg": "Exiting play if target hosts are unreachable"}
fatal: [remhost3]: FAILED! => {"changed": false, "msg": "Exiting play if target hosts are unreachable"}

PLAY RECAP *****************************************************************************************************************************************************************
remhost1                : ok=8    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
remhost3                : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
remhost2               : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0

Wednesday 13 March 2024  14:12:20 -0500 (0:00:00.030)       0:00:05.137 *******
===============================================================================
Gathering Facts ----------------------------------------------------------------------------------------------------------------------------------------------------- 4.46s
Perform ping in Template -------------------------------------------------------------------------------------------------------------------------------------------- 0.45s
debug --------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.04s
set_fact ------------------------------------------------------------------------------------------------------------------------------------------------------------ 0.04s
Display unreachable hosts one at a time ----------------------------------------------------------------------------------------------------------- 0.03s
fail ---------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.03s
debug --------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.02s
debug --------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.02s
Print group names --------------------------------------------------------------------------------------------------------------------------------------------------- 0.02s

As you see i get the desired output showing the host that was unreachable i.e below:

TASK [Display unreachable hosts one at a time] ***********************************************************************************************************
Wednesday 13 March 2024  14:12:20 -0500 (0:00:00.041)       0:00:05.074 *******
ok: [remhost1] => (item=remhost2) => {
    "msg": "Unreachable Host: remhost2"

I wish the similar output but this time my inventory host contains single host i.e remhost2 but I fail to get the desired output as you can see below.

It does not print "Unreachable Host: remhost2"

[wladmin@remhost1 ~]$ cat inventory.hosts

[remhosts]
remhost2

[wladmin@remhost1 ~]$ ansible-playbook -i inventory.hosts getur.yml

[DEPRECATION WARNING]: [defaults]callback_whitelist option, normalizing names to new standard, use callbacks_enabled instead. This feature will be removed from ansible-
core in version 2.15. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[WARNING]: ansible.utils.display.initialize_locale has not been called, this may result in incorrectly calculated text widths that can cause Display to print incorrect
line lengths

PLAY [Check unreachable hosts] *********************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************
Wednesday 13 March 2024  14:13:10 -0500 (0:00:00.009)       0:00:00.009 *******
fatal: [remhost2]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: \nThis system is for the use by authorized users only. All data contained\non all systems is owned by the company and may be monitored, intercepted,\nrecorded, read, copied, or captured in any manner and disclosed in any\nmanner, by authorized company personnel. Users (authorized or unauthorized)\nhave no explicit or implicit expectation of privacy. Unauthorized or improper\nuse of this system may result in administrative, disciplinary action, civil\nand criminal penalties. Use of this system by any user, authorized or\nunauthorized, constitutes express consent to this monitoring, interception,\nrecording, reading, copying, or capturing and disclosure.\n\nIF YOU DO NOT CONSENT, LOG OFF NOW.\n\n##################################################################\n# *** This Server is using Centrify                          *** #\n# *** Remember to use your Active Directory account          *** #\n# ***    password when logging in                            *** #\n##################################################################\n\nwladmin@remhost2: Permission denied (publickey,password,keyboard-interactive).", "unreachable": true}

PLAY RECAP *****************************************************************************************************************************************************************
remhost2               : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0

Wednesday 13 March 2024  14:13:15 -0500 (0:00:04.441)       0:00:04.450 *******
===============================================================================
Gathering Facts ----------------------------------------------------------------------------------------------------------------------------------------------------- 4.44s
[wladmin@remhost1 ~]$

Can you please suggest some solution so that i can get "Unreachable Host: remhost2" printed whether the inventory file contains one or more hosts.

1

There are 1 answers

3
Alexander Pletnev On

Ansible attempts to gather facts before running the tasks and obviously it can't run any tasks on a target host if it failed to connect to it. Besides, if the unreachable host is the only target host, there is nothing to run the tasks on, so Ansible cannot proceed.