-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloadbalanced
executable file
·66 lines (55 loc) · 1.59 KB
/
loadbalanced
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
#!/usr/bin/env python
# import ansible-specific magic
from ansible.module_utils.basic import *
import subprocess
def main():
module = AnsibleModule(
argument_spec=dict(
state=dict(required=True, choices=['enabled', 'disabled']),
backend=dict(default='app', required=False),
server=dict(required=True),
socket=dict(
default='/var/run/haproxy.sock', required=False
)
),
supports_check_mode=True
)
if module.check_mode:
module.exit_json(changed=False)
echo_cmd = ['echo',
'{0} server {1}/{2}'.
format(
module.params['state'][:-1],
module.params['backend'],
module.params['server']
)
]
echo_process = subprocess.Popen(
echo_cmd,
stdout=subprocess.PIPE
)
socket_cmd = ['socat', module.params['socket'], 'stdio']
socat = subprocess.Popen(
socket_cmd,
stdin=echo_process.stdout,
stdout=subprocess.PIPE
)
echo_process.stdout.close()
out, err = socat.communicate()
if socat.returncode != 0:
module.exit_json(
failed=True,
returncode=socat.returncode,
stdout=out,
stderr=err,
)
# doesn't look like there's a way to check the result of the enable/disable
# call as long as its well-formed
module.exit_json(
changed=True,
out=out,
err=err,
cmd=socket_cmd,
echo_cmd=echo_cmd
)
main()