forked from willthames/ansible-testing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassert_tcp
100 lines (92 loc) · 2.9 KB
/
assert_tcp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2014, Will Thames <[email protected]>
# (c) 2014, Allan Denot <[email protected]>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
DOCUMENTATION = '''
---
module: assert_tcp
version_added: 1.5
short_description: Check if a tcp connection can be made
description:
- Check if a TCP connection can be made to a host on a port
options:
name:
description:
- host to connect to, including port
required: True
default: null
should_be:
description:
- state of the port
choices: [ 'open', 'closed' ]
default: open
with_timeout:
description:
- time in seconds to wait for a response
required: False
default: 5
author: Will Thames
'''
EXAMPLES = '''
# Check if port 80 is open on a server
assert_tcp: name=localhost:80 should_be=open with_timeout=30
'''
import socket
def main():
module = AnsibleModule(
argument_spec = dict(
name=dict(required=True, default=None),
should_be=dict(choices=['open', 'closed'], required=True),
with_timeout=dict(required=False, default=5, type='int'),
),
supports_check_mode=True
)
host = module.params.get('name').split(":")[0]
port = module.params.get('name').split(":")[1]
state = module.params.get('should_be')
timeout = module.params.get('with_timeout')
s = None
for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error, msg:
s = None
continue
try:
s.settimeout(timeout)
s.connect(sa)
except socket.error, msg:
s.close()
s = None
continue
break
if s is None:
if state == 'open':
module.fail_json(msg='Port %s is closed on %s, it should be open' % (port, host))
if state == 'closed':
module.exit_json()
else:
if state == 'open':
module.exit_json()
if state == 'closed':
module.fail_json(msg='Port %s is open on %s, it should be closed' % (port,host))
s.close()
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()