Adding absent condition for to the wait_for module

This commit is contained in:
Andrii Radyk 2013-12-12 10:28:01 +02:00
parent 557ad6a411
commit 29c9922ac5

View file

@ -34,8 +34,8 @@ description:
which is true of certain Java application servers. It is also which is true of certain Java application servers. It is also
useful when starting guests with the M(virt) module and useful when starting guests with the M(virt) module and
needing to pause until they are ready. This module can needing to pause until they are ready. This module can
also be used to wait for a file to be available on the filesystem also be used to wait for a file to be available or absent on the
or with a regex match a string to be present in a file. filesystem or with a regex match a string to be present in a file.
version_added: "0.7" version_added: "0.7"
options: options:
host: host:
@ -60,10 +60,10 @@ options:
required: false required: false
state: state:
description: description:
- either C(present), C(started), or C(stopped) - either C(present), C(started), or C(stopped), C(absent)
- When checking a port C(started) will ensure the port is open, C(stopped) will check that it is closed - When checking a port C(started) will ensure the port is open, C(stopped) will check that it is closed
- When checking for a file or a search string C(present) or C(started) will ensure that the file or string is present before continuing - When checking for a file or a search string C(present) or C(started) will ensure that the file or string is present before continuing, C(absent) will check that file is absent or removed
choices: [ "present", "started", "stopped" ] choices: [ "present", "started", "stopped", "absent" ]
default: "started" default: "started"
path: path:
version_added: "1.4" version_added: "1.4"
@ -78,7 +78,7 @@ options:
notes: [] notes: []
requirements: [] requirements: []
author: Jeroen Hoekx, John Jarvis author: Jeroen Hoekx, John Jarvis, Andrii Radyk
''' '''
EXAMPLES = ''' EXAMPLES = '''
@ -92,6 +92,12 @@ EXAMPLES = '''
# wait until the string "completed" is in the file /tmp/foo before continuing # wait until the string "completed" is in the file /tmp/foo before continuing
- wait_for: path=/tmp/foo search_regex=completed - wait_for: path=/tmp/foo search_regex=completed
# wait until the lock file is removed
- wait_for: path=/var/lock/file.lock state=absent
# wait until the process is finished and pid was destroyed
- wait_for: path=/proc/3466/status state=absent
''' '''
def main(): def main():
@ -105,7 +111,7 @@ def main():
port=dict(default=None), port=dict(default=None),
path=dict(default=None), path=dict(default=None),
search_regex=dict(default=None), search_regex=dict(default=None),
state=dict(default='started', choices=['started', 'stopped', 'present']), state=dict(default='started', choices=['started', 'stopped', 'present', 'absent']),
), ),
) )
@ -133,11 +139,20 @@ def main():
if delay: if delay:
time.sleep(delay) time.sleep(delay)
if state == 'stopped': if state in [ 'stopped', 'absent' ]:
### first wait for the stop condition ### first wait for the stop condition
end = start + datetime.timedelta(seconds=timeout) end = start + datetime.timedelta(seconds=timeout)
while datetime.datetime.now() < end: while datetime.datetime.now() < end:
if path:
try:
f = open(path)
f.close()
time.sleep(1)
pass
except IOError:
break
elif port:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(connect_timeout) s.settimeout(connect_timeout)
try: try:
@ -149,7 +164,10 @@ def main():
break break
else: else:
elapsed = datetime.datetime.now() - start elapsed = datetime.datetime.now() - start
if port:
module.fail_json(msg="Timeout when waiting for %s:%s to stop." % (host, port), elapsed=elapsed.seconds) module.fail_json(msg="Timeout when waiting for %s:%s to stop." % (host, port), elapsed=elapsed.seconds)
elif path:
module.fail_json(msg="Timeout when waiting for %s to be absent." % (path), elapsed=elapsed.seconds)
elif state in ['started', 'present']: elif state in ['started', 'present']:
### wait for start condition ### wait for start condition