This repository has been archived by the owner on Apr 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
corosync_service
72 lines (64 loc) · 1.88 KB
/
corosync_service
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
def build_file(item, params):
tmpfd, temp_path = tempfile.mkstemp()
tmp = os.fdopen(tmpfd, 'w')
if item == 'service':
content = """
service {
name: %(name)s
ver: %(ver)s
}
""" % params
elif item == 'uidgid':
content = """
uidgid {
uid: %(user)s
gid: %(group)s
}
""" % params
tmp.write(content)
tmp.close()
return temp_path
def main():
module = AnsibleModule(
argument_spec = dict(
state = dict(default='present', choices=['present', 'absent']),
name = dict(required=True),
ver = dict(required=True),
user = dict(default='root'),
group = dict(default='root'),
),
supports_check_mode=True,
)
name = module.params['name']
state = module.params['state']
paths = dict(
service = '/etc/corosync/service.d/%s' % name,
uidgid = '/etc/corosync/uidgid.d/%s' % name,
)
if state == 'absent':
for path in paths.itervalues():
if not os.path.exists(path):
module.exit_json(path=path, changed=False)
else:
if not module.check_mode:
os.unlink(service_path)
module.exit_json(path=path, changed=True)
else:
changed = False
for item, dest in paths.iteritems():
destmd5 = None
path = build_file(item, module.params)
pathmd5 = module.md5(path)
if os.path.exists(dest):
destmd5 = module.md5(dest)
if pathmd5 != destmd5:
changed = True
if not module.check_mode:
shutil.copy(path, dest)
os.remove(path)
module.exit_json(changed=changed, msg='OK')
# import module snippets
from ansible.module_utils.basic import *
main()