blob: ebc3d51fa5e8a6eaef860e98f6996fa472041630 (
plain)
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
|
#!/usr/bin/env python
import json
import sys
import shlex
try:
import commentjson
json_load = commentjson.load
except ImportError:
json_load = json.load
def main(params_list):
params = dict(x.split("=", 1) for x in params_list)
path = params.pop('path')
changed = False
for key in params.keys():
if key.startswith('_ansible_'):
params.pop(key)
with open(path) as f:
obj = json_load(f)
for (key, target) in params.items():
parts = key.split('.')
ref = obj
for part in parts[:-1]:
if part not in ref:
ref[part] = {}
ref = ref[part]
last_part = parts[-1]
if target == 'unset':
if last_part in ref:
del ref[last_part]
changed = True
else:
if target.isdigit():
target = int(target)
if target == 'null':
target = None
if target == 'false':
target = False
if target == 'true':
target = True
if last_part not in ref or ref[last_part] != target:
ref[last_part] = target
changed = True
if changed:
with open(path, 'w') as f:
json.dump(obj, f, indent=2, separators=(',', ': '), sort_keys=True)
print(json.dumps({'changed': changed}))
if __name__ == '__main__':
if len(sys.argv) == 2:
main(shlex.split(open(sys.argv[1]).read()))
else:
main(sys.argv[1:])
|