我使用以下攻略检查从目标主机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'根据输出结果,我还有剩余的任务要执行。有没有更好的方法来验证连通性?
发布于 2021-04-26 09:22:06
你可以通过测试rc来简化代码,例如
- command: ping -c3 8.8.8.8
ignore_errors: true
register: output
- meta: end_host
when: output.rc != 0如果您可以在192.168.153.31上运行攻略,并且想要测试Ansible是否可以连接到目标系统,请使用wait_for_connection,例如
---
- hosts: 8.8.8.8
gather_facts: no
tasks:
- name: Wait for connection to net1
wait_for_connection:https://stackoverflow.com/questions/67257168
复制相似问题