Commit f0da81b1 by Alain Takoudjou

Add task to ping one or multiple hosts

parent 0ee609e5
import os
import sys
from subprocess import Popen, PIPE
def check_ping(host_list):
for host in host_list:
process = Popen("ping -c 1 %s" % host, shell=True, stdout=PIPE)
result = process.communicate()[0]
if process.returncode == 0:
raise Exception('PING fail: host at %s didn\'t send response.\n%s' % (
host, result))
def ping_cluster(hpath):
if os.path.exists(hpath):
for content in open(hpath, 'r').readlines():
if content:
items = content.strip().split(' ')
if __name__ == "__main__":
if len(sys.argv) < 3:
print "Use: %s TYPE [HOST LIST] OR [HOST-FILE]" % sys.argv[0]
print "ex: %s host; %s cluster /tmp/hosts" % (
sys.argv[0], sys.argv[0])
if sys.argv[1] == 'host':
elif sys.argv[1] == 'cluster':
......@@ -46,7 +46,7 @@
- name: stat /tmp/hosts
stat: path=/tmp/hosts
register: hostname_file
register: hosts_file
- name: Format hosts
script: format_hosts /tmp/hosts
......@@ -57,3 +57,12 @@
- name: adding entry from workspace
lineinfile: dest=/etc/resolv.conf line="nameserver"
- name: ping current host
script: ping host {{ lookup('file', '/etc/opt/ipv4') }} {{ lookup('file', '/etc/opt/hostname') }}
when: hostname_file.stat.exists == True
- name: ping cluster hosts
script: ping cluster /tmp/hosts
when: hosts_file.stat.exists == True
ignore_errors: True
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment