在ansible中检测无法访问主机的经典解决方案不适用于单个主机

问题描述 投票:0回答:1

我的 ansible 库存有 3 台主机,其中一台无法访问,即

remhost2

下面是所需的工作手册以及所需的输出。

[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

运行并期望输出:

[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

如您所见,我得到了所需的输出,显示无法访问的主机,即如下:

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"

我希望有类似的输出,但这次我的库存主机包含单个主机,即

remhost2
但我无法获得所需的输出,如下所示。

不打印

"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 ~]$

您能否建议一些解决方案,以便无论库存文件包含一个还是多个主机,我都可以打印

"Unreachable Host: remhost2"

ansible connection ansible-inventory ansible-facts
1个回答
-1
投票

只需禁用此剧本中的事实收集即可。 Ansible 尝试在运行任务之前收集事实,显然如果无法连接到目标主机,它就无法在目标主机上运行任何任务。

© www.soinside.com 2019 - 2024. All rights reserved.