2
# -*- coding: utf-8 -*-
4
# (c) 2012, Jeroen Hoekx <jeroen@hoekx.be>
6
# This file is part of Ansible
8
# Ansible is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# Ansible is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
29
short_description: Waits for a given port to become accessible on a server.
31
- This is useful for when services are not immediately available after
32
their init scripts return - which is true of certain Java application
33
servers. It is also useful when starting guests with the M(virt) module and
34
needing to pause until they are ready.
39
- hostname or IP address to wait for
45
- maximum number of seconds to wait for
50
- number of seconds to wait before starting to poll
59
- either C(started), or C(stopped) depending on whether the module should
60
poll for the port being open or closed.
61
choices: [ "started", "stopped" ]
64
- code: "wait_for: port=8000 delay=10"
65
description: "Example from Ansible Playbooks"
73
module = AnsibleModule(
75
host=dict(default='127.0.0.1'),
76
timeout=dict(default=300),
77
connect_timeout=dict(default=5),
78
delay=dict(default=0),
79
port=dict(required=True),
80
state=dict(default='started', choices=['started', 'stopped']),
84
params = module.params
87
timeout = int(params['timeout'])
88
connect_timeout = int(params['connect_timeout'])
89
delay = int(params['delay'])
90
port = int(params['port'])
91
state = params['state']
96
start = datetime.datetime.now()
98
if state == 'stopped':
99
### first wait for the host to go down
100
end = start + datetime.timedelta(seconds=timeout)
102
while datetime.datetime.now() < end:
103
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
104
s.settimeout(connect_timeout)
106
s.connect( (host, port) )
107
s.shutdown(socket.SHUT_RDWR)
113
elapsed = datetime.datetime.now() - start
114
module.fail_json(msg="Timeout when waiting for %s:%s to stop." % (host, port), elapsed=elapsed.seconds)
116
elif state == 'started':
117
### wait for the host to come up
118
end = start + datetime.timedelta(seconds=timeout)
120
while datetime.datetime.now() < end:
121
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
122
s.settimeout(connect_timeout)
124
s.connect( (host, port) )
125
s.shutdown(socket.SHUT_RDWR)
132
elapsed = datetime.datetime.now() - start
133
module.fail_json(msg="Timeout when waiting for %s:%s" % (host, port), elapsed=elapsed.seconds)
135
elapsed = datetime.datetime.now() - start
136
module.exit_json(state=state, port=port, elapsed=elapsed.seconds)
138
# this is magic, see lib/ansible/module_common.py
139
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>