当主机不可达时,跳过失败模块。

4

我正在尝试在某些主机不可达时打印自定义消息。我的问题是,当主机不可达时,它将在下一个任务中被跳过,因此失败模块永远不会被触发。 而可达的主机也会由于when条件未满足而跳过任务。

我尝试了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
1个回答

5

你正在寻找关键字ignore_unreachable,它应该被设置为true,因为你确实想要忽略无法到达的主机,以便前往你的fail任务。

给定剧本:

- hosts: nodes
  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

这将产生:

TASK [ping] *******************************************************
fatal: [node1]: UNREACHABLE! => changed=false 
  msg: 'Failed to connect to the host via ssh'
  skip_reason: Host node1 is unreachable
  unreachable: true
fatal: [node2]: UNREACHABLE! => changed=false 
  msg: 'Failed to connect to the host via ssh'
  skip_reason: Host node2 is unreachable
  unreachable: true
fatal: [node3]: UNREACHABLE! => changed=false 
  msg: 'Failed to connect to the host via ssh'
  skip_reason: Host node3 is unreachable
  unreachable: true

TASK [fail] *******************************************************
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 ************************************************  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接