简体   繁体   English

需要首先 ssh 并使用 ansible-playbook ping

[英]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

  1. if ssh make then print Y other No如果 ssh make 然后打印 Y other No
  2. if ssh make Y then try to perform Ping and print Y along with uptime如果 ssh 使 Y 然后尝试执行 Ping 并打印 Y 以及正常运行时间
  3. if ping getting not access then print N如果 ping 无法访问,则打印 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 'ansible-playbook' 在 Ubuntu 安装 Exim4 - Installing Exim4 in Ubuntu using 'ansible-playbook' 如何使用 Ansible-playbook 在磁盘的“/etc/fstab”中进行 append 条目? - How to append entries in `/etc/fstab` of disks using Ansible-playbook? 如何在嵌套的 for 循环中使用 ansible-playbook? - How to use ansible-playbook in a nested for loop? 删除所有旧文件,但使用 ansible-playbook 保留最新的 4 个文件 - Delete all old files, but keep newest 4 files using ansible-playbook ansible-playbook 从命令行传递的额外变量 - ansible-playbook extra vars passing from command line 如何从 python3 支持 ansible-playbook - how to support ansible-playbook from python3 使用已定义的参数规范验证传递给 ansible-playbook 的 arguments - Validates arguments passed to ansible-playbook with a defined argument specification ansible-通过ansible-playbook执行InstallHalyard.sh脚本时没有任何反应 - ansible - Nothing happens when executing InstallHalyard.sh script via ansible-playbook 如何将值传递给列表中的变量,使用命令“ansible-playbook test.yml --extra-var variable1=ntpd,crond,sysconf”一个一个地选择 - How to pass value to variable in list, to pick one by one using command "ansible-playbook test.yml --extra-var variable1=ntpd,crond,sysconf" 在 EC2 实例上使用用户数据脚本运行 ansible-playbook - Run ansible-playbook with a user-data script on an EC2 instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM