-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimplefile.py
63 lines (51 loc) · 1.52 KB
/
simplefile.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
import os
from tempfile import gettempdir
from ansible.module_utils.basic import AnsibleModule
class File:
def __init__(self, path):
self.path = path
def exists(self):
return os.path.exists(self.path)
def touch(self):
with open(self.path, 'a'):
os.utime(self.path, None)
def remove(self):
if self.exists():
os.remove(self.path)
return True
else:
return False
class SimpleFileModule(object):
def __init__(self, expected):
self.expected = expected
self.file = File(expected['path'])
def ensure(self):
if self.expected['state'] == 'present':
return self.__ensure_present()
else:
return self.__ensure_absent()
def __ensure_present(self):
if self.file.exists():
return (False, self.expected)
else:
self.file.touch()
return (True, self.expected)
def __ensure_absent(self):
return (self.file.remove(), self.expected)
def main():
module = AnsibleModule(
argument_spec=dict(
path=dict(required=True, type='path'),
state=dict(choices=['present', 'absent'], default=None)
)
)
simplefile = SimpleFileModule(
expected=dict(
path=module.params['path'],
state=module.params['state']
)
)
changed, params = simplefile.ensure()
module.exit_json(changed=changed, simplefile=params)
if __name__ == '__main__':
main()