-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_remote_host.py
114 lines (90 loc) · 2.8 KB
/
record_remote_host.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python
DOCUMENTATION = '''
---
module: record_remote_host
short_description: This will record remote hostname where playbook ran.
description:
- "This module will record all the hosts from inventory where playbook was run"
options:
remotehost:
description:
- remotehost name passed as parameter while calling this module
required: true
'''
EXAMPLES = '''
- name: record remote host
- record_remote_host: remotehost="{{ inventory_hostname }}"
'''
from ansible.module_utils.basic import AnsibleModule
import json
import sys
import os.path
import fileinput, re
import socket
from os import path
_FILENAME_CFG = "filename.cfg"
def read_cfg():
try:
cfgfile = file(_FILENAME_CFG)
for line in cfgfile:
if "filename=" in line:
name, value = line.split("=", 1)
return value
break
except Exception as e:
module.fail_json(msg='Error read_cfg')
filename = read_cfg()
""" check string i.e. run-on already in file """
def check_string():
try:
datafile = file(filename)
for line in datafile:
if "run-on" in line:
return True
datafile.close()
break
except Exception as e:
module.fail_json(msg='Error string_check')
def main():
module = AnsibleModule(
argument_spec = dict(
remotehost = dict(required=True, type='str'),
)
)
remotehost = module.params['remotehost']
data = dict(
output="remotehost stored successfuly",
)
try:
if os.path.exists(filename): # open file to append
if check_string():
with open(filename, 'r') as file:
file_data = file.read()
upd_data = file_data.replace(']', ',' + '"{}"'.format(remotehost) + "]")
with open(filename, 'w') as file:
file.write(upd_data)
file.close()
else:
with open(filename, 'a') as outfile:
outfile.seek(-1, os.SEEK_END)
print("after SEEK")
outfile.truncate()
outfile.write(',')
outfile.write('\n')
outfile.write("\"run-on\"" + ": [" + '"{}"'.format(remotehost))
outfile.write("]")
outfile.write("}")
outfile.close()
else: # create a new file
with open(filename, 'w') as outfile:
outfile.write("{")
outfile.write('\n')
outfile.write("\"run-on\"" + ": [" + '"{}"'.format(remotehost))
outfile.write("]")
outfile.write("}")
outfile.close()
except Exception as ex:
module.fail_json(msg='Error in record-remote-host')
module.exit_json(changed=True, success=data, msg=data)
if __name__ == '__main__':
main()