如何在Ansible中检测无法访问的目标主机

问题描述

如果目标主机sshreachable是否可访问,我希望获取变量all_hosts

我也是为下面的剧本写的。

- name: Play 3- check telnet nodes
  hosts: localhost
  ignore_unreachable: yes


   - name: Check all port numbers are accessible from current host
     include_tasks: innertelnet.yml
     with_items: "{{ groups['all_hosts'] }}"

cat innertelnet.yml

---

       - name: Check ssh connectivity
         block:

           - raw: "ssh -o BatchMode=yes root@{{ item }} echo success"
             ignore_errors: yes
             register: sshcheck

           - debug:
               msg: "SSHCHECK variable:{{ sshcheck }}"

           - set_fact:
               sshreachable: 'SSH SUCCESS'
             when: sshcheck.unreachable == 'false'
           - set_fact:
               sshreachable: 'SSH FAILED'
             when: sshcheck.unreachable == 'true'

       - debug:
           msg: "INNERSSH1: {{ sshreachable }}"

不幸的是,我收到如下错误:

输出:

TASK [raw] *********************************************************************
    fatal: [localhost]: UNREACHABLE! => {"changed": false,"msg": "Failed to connect to the host via ssh: Shared connection to 10.9.9.126 closed.","skip_reason": "Host localhost is unreachable","unreachable": true}
    
        TASK [debug] ***********************************************************************************************************************************************************
        task path:

    ok: [localhost] => {
        "msg": "SSHCHECK variable:{'msg': u'Failed to connect to the host via ssh: Shared connection to 10.9.9.126 closed.','unreachable': True,'changed': False}"
    }

TASK [set_fact] ****************************************************************
skipping: [localhost]

TASK [set_fact] ****************************************************************
skipping: [localhost]

TASK [debug] *******************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'sshreachable' is undefined\n\nThe error appears to be in '/app/playbook/checkssh/innertelnet.yml': line 45,column 10,but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n       - debug:\n         ^ here\n"}

PLAY RECAP *********************************************************************
10.0.116.194                : ok=101  changed=1    unreachable=9    failed=0    skipped=12   rescued=0    ignored=95  
localhost          : ok=5    changed=0    unreachable=1    failed=1    skipped=4    rescued=0    ignored=0  

您能否建议对我的代码进行更改以使其正常工作?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)