首页 > 解决方案 > 当主机无法访问时,ansible跳过失败模块

问题描述

当某些主机无法访问时,我正在尝试打印自定义消息。我的问题是,当主机不可达时,它将在下一个任务中被跳过,因此永远不会触发失败模块。并且可访问的主机也将由于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

标签: ansibleansible-tower

解决方案


您正在寻找ignore_unreachable应设置为 的关键字 ,true因为您确实想忽略无法访问的主机以继续执行您的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   

推荐阅读