Merge branch 'wait-for-port-search-regex' of https://github.com/sivel/ansible into sivel-wait-for-port-search-regex

reviewable/pr18780/r1
James Cammarata 10 years ago
commit 1abf10a4f6

@ -76,9 +76,9 @@ options:
version_added: "1.4" version_added: "1.4"
required: false required: false
description: description:
- with the path option can be used match a string in the file that must match before continuing. Defaults to a multiline regex. - Can be used to match a string in either a file or a socket connection. Defaults to a multiline regex.
notes:
notes: [] - The ability to use search_regex with a port connection was added in 1.7.
requirements: [] requirements: []
author: Jeroen Hoekx, John Jarvis, Andrii Radyk author: Jeroen Hoekx, John Jarvis, Andrii Radyk
''' '''
@ -100,6 +100,9 @@ EXAMPLES = '''
# wait until the process is finished and pid was destroyed # wait until the process is finished and pid was destroyed
- wait_for: path=/proc/3466/status state=absent - wait_for: path=/proc/3466/status state=absent
# Wait 300 seconds for port 22 to become open and contain "OpenSSH", don't start checking for 10 seconds
- local_action: wait_for port=22 host="{{ inventory_hostname }}" search_regex=OpenSSH delay=10
''' '''
def main(): def main():
@ -205,16 +208,32 @@ def main():
s.settimeout(connect_timeout) s.settimeout(connect_timeout)
try: try:
s.connect( (host, port) ) s.connect( (host, port) )
s.shutdown(socket.SHUT_RDWR) if search_regex:
s.close() data = ''
break matched = False
while 1:
data += s.recv(1024)
if re.search(search_regex, data, re.MULTILINE):
matched = True
break
if matched:
s.shutdown(socket.SHUT_RDWR)
s.close()
break
else:
s.shutdown(socket.SHUT_RDWR)
s.close()
break
except: except:
time.sleep(1) time.sleep(1)
pass pass
else: else:
elapsed = datetime.datetime.now() - start elapsed = datetime.datetime.now() - start
if port: if port:
module.fail_json(msg="Timeout when waiting for %s:%s" % (host, port), elapsed=elapsed.seconds) if search_regex:
module.fail_json(msg="Timeout when waiting for search string %s in %s:%s" % (search_regex, host, port), elapsed=elapsed.seconds)
else:
module.fail_json(msg="Timeout when waiting for %s:%s" % (host, port), elapsed=elapsed.seconds)
elif path: elif path:
if search_regex: if search_regex:
module.fail_json(msg="Timeout when waiting for search string %s in %s" % (search_regex, path), elapsed=elapsed.seconds) module.fail_json(msg="Timeout when waiting for search string %s in %s" % (search_regex, path), elapsed=elapsed.seconds)

Loading…
Cancel
Save