-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_end_time.py
82 lines (62 loc) · 1.85 KB
/
record_end_time.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
#!/usr/bin/python
DOCUMENTATION = '''
---
module: record_end_time
short_description: This will record start time when playbook started.
options:
None
'''
EXAMPLES = '''
- name: record end time
- record_end_time:
'''
from ansible.module_utils.basic import AnsibleModule
import json
import sys
import os.path
import fileinput, re
import socket
from os import path
from datetime import datetime
_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()
def main():
module = AnsibleModule(
argument_spec=dict({})
)
end_time = datetime.utcnow().strftime("%d-%b-%Y %H:%M:%S")
data = dict(
output="end-time stored called successfully",
)
try:
if os.path.exists(filename): # open file to append
with open(filename, 'a') as outfile:
outfile.seek(-1, os.SEEK_END)
outfile.truncate()
outfile.write(',')
outfile.write('\n')
outfile.write("\"end-time\"" + ": " + '"{}"'.format(end_time))
outfile.write("}")
outfile.close()
else: # create a new file
with open(filename, 'w') as outfile:
outfile.write("{")
outfile.write('\n')
outfile.write("\"end-time\"" + ": " + '"{}"'.format(end_time))
outfile.write("}")
outfile.close()
except Exception as e:
module.fail_json(msg='Error in record-start-time')
module.exit_json(changed=True, success=data, msg=end_time)
if __name__ == '__main__':
main()