如何在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  

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

解决方法

错误似乎表明sshreachable变量未设置,因为when:条件不匹配。即sshcheck.unreachable可能不是raw返回的内容。

为此,command模块就足够了,我们可以评估到set_fact的命令返回代码。

您可以执行以下操作:

- block:
  - command: ssh -o BatchMode=yes user@host1 echo success
    ignore_errors: yes
    register: sshcheck
  - set_fact:
      sshreachable: "{{ sshcheck is success }}"

- debug:
    msg: "Host1 reachable: {{ sshreachable | string }}"

更新:

raw模块似乎以相同的方式工作。示例(包括 @mdaniel 的宝贵输入):

- block:
  - raw: ssh -o BatchMode=yes user@host1 echo success
    ignore_errors: yes
    register: sshcheck
  - set_fact:
      sshreachable: SSH SUCCESS
    when: sshcheck is success
  - set_fact:
      sshreachable: SSH FAILED
    when: sshcheck is failed

- debug:
    msg: "Host1 reachable: {{ sshreachable }}"