需要先用ansible-playbook进行ssh和ping

0 人关注

假设我有100台主机,我想进行一个ssh连接,结果应该打印 "YES "或 "NO"

然后进行一次ping,应该打印 "YES "或 "NO"。

  • if ssh make then print Y other No
  • if ssh make Y then try to perform Ping and print Y along with uptime
  • if ping getting not access then print N
  • 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  
        
    4 个评论
    一旦你用SSH连接了服务器,那么为什么你需要再次ping?只有当目标服务器上的22号端口被打开时,你才能SSH和PING...
    那么到目前为止你都尝试了什么?你在哪里被卡住了?你可能想阅读 如何提问
    - Pradeep Kumar 首先,我需要检查ping,如果ping成功,然后尝试SSH和Uptime,然后退出。
    如果ping不成功,则打印状态No和ssh为no或--对正常运行时间也是如此。
    linux
    ssh
    ansible
    redhat
    ping
    Avinash Jadhav
    Avinash Jadhav
    发布于 2020-05-28
    1 个回答
    Pradeep Kumar H C
    Pradeep Kumar H C
    发布于 2020-05-30
    已采纳
    0 人赞同

    这里有一个可能的方法来实现它。

    - 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: