【问题标题】:How to detect UNREACHABLE HOSTS in Ansible如何在 Ansible 中检测 UNREACHABLE HOSTS
【发布时间】:2020-09-10 04:16:29
【问题描述】:

下面是我的剧本

- name: Play 1.5 - Check each target
  hosts: all_hosts
  ignore_unreachable: yes
  ignore_errors: yes
  gather_facts: true

  tasks:

   - raw: "echo {{ inventory_hostname }} is UNREACHABLE"
     delegate_to: localhost
     when: <Need help with the when condition here>

我需要有关上述剧本中 when 条件的帮助。

当我对无法访问的主机运行游戏时,调试输出清楚地显示输出是 JSON 格式,并且必须有一个变量可以捕获 inventory_host connection status

请看下面的输出:

TASK [Gathering Facts] *************************************************************************************************************************************************
task path: /app/Ansible/playbook/check/check.yml:55
<10.9.80.111> Attempting python interpreter discovery
<10.9.80.111> ESTABLISH SSH CONNECTION FOR USER: root
<10.9.80.111> SSH: EXEC ssh -o 'IdentityFile="/app/automation/ssh_keys/id_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="root"' -o ConnectTimeout=10 -o StrictHostKeyChecking=no 10.9.80.111 '/bin/sh -c '"'"'echo PLATFORM; uname; echo FOUND; command -v '"'"'"'"'"'"'"'"'/usr/bin/python'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.7'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.6'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.5'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python2.7'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python2.6'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/libexec/platform-python'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/bin/python3'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python'"'"'"'"'"'"'"'"'; echo ENDFOUND && sleep 0'"'"''
<10.9.80.111> (255, '', 'ssh: connect to host 10.9.80.111 port 22: Connection timed out\r\n')
[WARNING]: Unhandled error in Python interpreter discovery for host 10.9.80.111: Failed to connect to the host via ssh: ssh: connect to host 10.9.80.111 port 22:
Connection timed out

Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/setup.py
Pipelining is enabled.
<10.9.80.111> ESTABLISH SSH CONNECTION FOR USER: root
<10.9.80.111> SSH: EXEC ssh -o 'IdentityFile="/app/axmw/misc_automation/ssh_keys/axmw_id_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="root"' -o ConnectTimeout=10 -o StrictHostKeyChecking=no 10.9.80.111 '/bin/sh -c '"'"'/usr/bin/python && sleep 0'"'"''
fatal: [10.9.80.111]: UNREACHABLE! => {
    "changed": false,
    "msg": "Data could not be sent to remote host \"10.9.80.111\". Make sure this host can be reached over ssh: ssh: connect to host 10.9.80.111 port 22: Connection timed out\r\n",
    "skip_reason": "Host 10.9.80.111 is unreachable",
    "unreachable": true
}
META: ran handlers 

从上面的输出我想得到具有以下值的变量:

fatal: [10.9.80.111]: UNREACHABLE! => {
    "changed": false,
    "msg": "Data could not be sent to remote host \"10.9.80.111\". Make sure this host can be reached over ssh: ssh: connect to host 10.9.80.111 port 22: Connection timed out\r\n",
    "skip_reason": "Host 10.9.80.111 is unreachable",
    "unreachable": true

因此,我希望从那里捕获unreachable": true 状态。

有人可以指导吗?

【问题讨论】:

    标签: ansible connection failed-to-connect


    【解决方案1】:

    你可以使用changed_when,当更改为false时,获取无法访问的主机

    - name: Test connection and gather facts
      hosts: all
      serial: 1
      gather_facts: true
      ignore_unreachable: yes
      become: false
      tasks:
    
      - name: Test connection
        shell: hostname
        register: connection_output
        ignore_unreachable: yes
    
      - debug: var=connection_output.changed
        ignore_errors: yes
    
    
    
      - name: print the list of unreachable servers
        lineinfile:
          line: "{{ connection_output.msg }}"
          dest: "/tmp/AnsibleConnectionCheck.txt"
          insertafter: EOF
        become: false
        delegate_to: 127.0.0.1
        run_once: true
        ignore_errors: yes
        changed_when: False
    
    

    今天我刚刚完成了这本剧本:) 希望它对你有所帮助!

    【讨论】:

    • 您好,您知道如何同时捕获 Failed 吗?我尝试使用 failed_when 的相同方法无济于事
    【解决方案2】:

    非常感谢刘薇薇!我一直在到处寻找这个。这是我使用 win_ping 实现相同的逻辑:

    ---
    
    - hosts: all
      gather_facts: no
      become: yes
    
      tasks:
    
      - name: Win_Ping
        win_ping:
        register: WinPingResult
        ignore_unreachable: yes
    
      - debug: var=WinPingResult.changed
        ignore_errors: yes
    
      - name: Printing errors
        debug:
          msg: "{{ WinPingResult.msg }}"
        run_once: yes
        changed_when: False
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 2018-08-31
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多