简体   繁体   中英

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"

Then perform a ping that should print "YES" or "NO"

  1. if ssh make then print Y other No
  2. if ssh make Y then try to perform Ping and print Y along with uptime
  3. if ping getting not access then print N

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM