当主机无法访问时 ansible 跳过失败模块

ansible skip fail module when host is unreachable

当无法访问某个主机时,我正在尝试打印一条自定义消息。我的问题是,当主机无法访问时,下一个任务将跳过它,因此 fail 模块永远不会被触发。 由于 when condition are not met.

,可访问的主机也将跳过任务

我试过了ignore_unreachable: false,还是一样。 任何想法将不胜感激。

---
- hosts: prod
  gather_facts: no
  tasks:
  - name: fail when not reachable
    action: ping
    register: ping_result
#    any_errors_fatal: true

  - fail:
      msg: "please make sure all server up and run again"
    when: ping_result.unreachable is defined
    any_errors_fatal: true

您正在查找关键字 ignore_unreachable,应该设置为 true,因为您 do 想要忽略无法访问的主机以继续执行您的 fail 任务。

鉴于剧本:

- hosts: all
  gather_facts: no

  tasks:
    - ping:
      register: ping_result
      ignore_unreachable: true

    - fail:
        msg: "please make sure all server up and run again"
      when: ping_result.unreachable is defined
      any_errors_fatal: true

这产生:

PLAY [all] **********************************************************************************************************

TASK [ping] *********************************************************************************************************
fatal: [node1]: UNREACHABLE! => {"changed": false, "msg": "[Errno -2] Name does not resolve", "skip_reason": "Host node1 is unreachable", "unreachable": true}
fatal: [node2]: UNREACHABLE! => {"changed": false, "msg": "[Errno -2] Name does not resolve", "skip_reason": "Host node2 is unreachable", "unreachable": true}
fatal: [node3]: UNREACHABLE! => {"changed": false, "msg": "[Errno -2] Name does not resolve", "skip_reason": "Host node3 is unreachable", "unreachable": true}
ok: [localhost]

TASK [fail] *********************************************************************************************************
skipping: [localhost]
fatal: [node1]: FAILED! => {"changed": false, "msg": "please make sure all server up and run again"}
fatal: [node2]: FAILED! => {"changed": false, "msg": "please make sure all server up and run again"}
fatal: [node3]: FAILED! => {"changed": false, "msg": "please make sure all server up and run again"}

NO MORE HOSTS LEFT **************************************************************************************************

PLAY RECAP **********************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
node1                      : ok=0    changed=0    unreachable=1    failed=1    skipped=1    rescued=0    ignored=0   
node2                      : ok=0    changed=0    unreachable=1    failed=1    skipped=1    rescued=0    ignored=0   
node3                      : ok=0    changed=0    unreachable=1    failed=1    skipped=1    rescued=0    ignored=0