Loops 当IP';可到达的

Loops 当IP';可到达的,loops,ansible,Loops,Ansible,我在剧本中有一个场景,当我看到某个节点启动时,它应该失败,并向用户发出警告消息“关闭节点,尝试重新运行”。我使用了失败模块,但不确定要添加哪个条件 - command: ping -c3 {{ server1-ip }} tags: cluster - command: ping -c3 {{ server2-ip }} tags: cluster - debug: msg: "The nodes {{ server1-ip }} and {{ server2-ip }} are

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

- 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

感谢您的帮助

我尝试了下面的逻辑及其工作原理

- 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将失败。我很欣赏其他的逻辑

我现在尝试了下面的逻辑及其工作原理

- 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将失败。我很欣赏其他的逻辑

根据我们在评论中的讨论,我们有两个剧本。第一个确保目标主机关闭,第二个从
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

根据我们在评论中的讨论,我们有两个剧本。第一个确保目标主机关闭,第二个从
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

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