[英]Need to first ssh and ping using ansible-playbook
Suppose I have 100 hosts and I want to perform a ssh connection where the result should print "YES" or "NO"假设我有 100 台主机,我想执行 ssh 连接,结果应该打印“YES”或“NO”
Then perform a ping that should print "YES" or "NO"然后执行应打印“YES”或“NO”的 ping
For example例如
SL.No. Server Ping SSH Uptime(hrs)
1 Linux-test y y 2020-26-05 17:17:44
2 linux-test1 n - -
3 linux-test3 y y 2020-26-05 17:17:44
here is a possible way to achieve it.这是实现它的一种可能方法。
- hosts: all
gather_facts: False
ignore_errors: True
ignore_unreachable: True
tasks:
- name: ping server
ping:
register: ping_status
- debug:
msg: "{{ inventory_hostname }} - YES"
with_items:
- "{{ inventory_hostname }}"
when: ping_status.ping is defined
- name: get uptime
shell: uptime > /root/uptime.out
- name: read uptime
command: cat /root/uptime.out
register: uptime_server
- debug:
msg: "{{ inventory_hostname }} - YES - {{ uptime_server.stdout }}"
with_items:
- "{{ inventory_hostname }}"
when: ping_status.ping is defined
- debug:
msg: "{{ inventory_hostname }} - NO"
with_items:
- "{{ inventory_hostname }}"
when: ping_status.ping is undefined
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.