【问题标题】:ansible playbook to check server reachability from target hostansible playbook 用于检查目标主机的服务器可达性
【发布时间】:2021-04-25 18:56:10
【问题描述】:

我正在使用以下 playbook 来检查从目标主机 192.168.153.31 到 8.8.8.8 的连接

---
- hosts: 192.168.153.31
  gather_facts: no

  tasks:
  - name: Ping net1
    shell:  |
      ping -c3 8.8.8.8 >/dev/null
      if [ $? -eq 0 ]
      then
        echo "ok"
      else
        echo "nok"
      fi
    ignore_errors: true
    register: output

  - name: end playbook
    meta: end_host
    when: output.stdout == 'nok'

根据输出,我还有剩余的任务要执行。有没有更好的方法来验证连接性?

【问题讨论】:

    标签: ansible


    【解决方案1】:

    你可以通过测试rc来简化代码,例如

        - command: ping -c3 8.8.8.8
          ignore_errors: true
          register: output
        - meta: end_host
          when: output.rc != 0
    

    如果您可以在 192.168.153.31 运行 playbook 并想测试 Ansible 是否可以连接到目标,请使用 wait_for_connection,例如

    ---
    - hosts: 8.8.8.8
      gather_facts: no
    
      tasks:
        - name: Wait for connection to net1
          wait_for_connection:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-19
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 2021-11-04
      • 1970-01-01
      相关资源
      最近更新 更多