Suppose I have 100 hosts and I want to perform a ssh connection where the result should print "YES" or "NO"
Then perform a ping that should print "YES" or "NO"
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.