【问题标题】:Ansible module to fail when IP's are reachable当 IP 可达时 Ansible 模块失败
【发布时间】:2020-05-21 18:41:32
【问题描述】:

当我看到某个节点启动时,我在剧本中有一个场景,它应该失败向用户发出警告消息“关闭节点正在尝试重新运行”。我使用了失败模块但不确定哪个条件添加。

- command: ping -c3 {{ server1-ip }}
 tags: cluster

- command: ping -c3 {{ server2-ip }}
 tags: cluster

- debug:
   msg: "The nodes {{ server1-ip }} and {{ server2-ip }} are pingable, please make sure to turn off the node before re-run else cluster fails"
   verbosity: 1
 tags: cluster

- name: Fail if servers are pingable
 fail:
   msg: "{{ server1-ip }} and {{ server2-ip }} are ON , make sure to turn off all nodes 
         before deployment"
 when: << >> #what conditional fits better here? 
 tags: cluster

现在,我正在考虑使用 command 模块的 o/p,这意味着如果我得到 0% 的损失,这意味着它的 ping。所以想到包括

- name: Test the connectivity for Servers
  command: ping -c3 {{ server1-ip }}
  register: ping_results
  failed_when: "0% packet loss in ping_results"       
  tags: cluster

感谢帮助

【问题讨论】:

  • 请向我们展示您到目前为止所做的尝试。
  • 首先,将ping 模块与failed_when: false 一起使用。然后将when: ping_results.rc == 0 放入你的失败模块中。
  • @Jack 谢谢,是的,这是另一种测试方式,当你说failed_when: false 时,这意味着ping 结果是成功的,接下来你使用另一个模块来说明它失败的原因。
  • 对。在您的情况下,您实际上希望 ping 模块失败,因此失败实际上就是成功!
  • 是的,这正是我的用例。

标签: loops ansible


【解决方案1】:

根据我们在 cmets 中的讨论,我们有两个剧本。第一个确保目标主机关闭,第二个从localhost 运行安装:

- name: Ensure all target hosts are down
  hosts: all
  gather_facts: no
  tasks:
  - name: ping hosts
    ping:
    failed_when: false
    ignore_unreachable: true
    register: ping_result

  - name: fail if host is up
    fail:
      msg: "{{ inventory_hostname }} is alive."
    when: not ping_result.unreachable
    any_errors_fatal: true

- name: Run install from localhost
  hosts: localhost
  gather_facts: no
  tasks:
  - name: This is where we would run install
    meta: noop

【讨论】:

    【解决方案2】:

    我尝试了下面的逻辑和它现在的工作,

    - name: Test the connectivity for Servers
      command: ping -c3 {{ server1-ip }}
      register: results
      failed_when:
        - '"0% packet loss" in results.stdout'    
      tags: cluster
    

    我们也可以使用:

    - name: Test the connectivity for servers
      command: ping -c3 {{ server1-ip }}
      register: results
      tags: cluster
    
    - name: If servers are ON fail with an error message
      fail:
        msg: "{{ server1-ip }} and {{ server2-ip }} are ON please switch it off"
      when: '"0% packet loss" in results.stdout'
      tags: cluster
    
    

    这意味着如果上面的服务器都启动了,这个 playbook 就会失败。我会欣赏任何其他逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 2015-08-15
      • 2018-04-23
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多