forked from 14rcole/oasis-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace.py
More file actions
55 lines (44 loc) · 1.54 KB
/
Copy pathreplace.py
File metadata and controls
55 lines (44 loc) · 1.54 KB
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
#!/usr/bin/env python
import yaml
import sys
import re
import yamlordereddictloader
if len(sys.argv) != 2:
sys.exit(1)
class AnsibleDumper(yamlordereddictloader.Dumper):
# insert blank lines between tasks
def write_line_break(self, data=None):
super().write_line_break(data)
if len(self.indents) == 1:
super().write_line_break()
with open(sys.argv[1], 'r') as ansibleFile:
tasks = yaml.load(ansibleFile, Loader=yamlordereddictloader.Loader)
for task in tasks:
if 'include' in task.keys():
new_include = 'include_tasks'
if 'static' in task.keys():
if task['static']:
new_include = 'import_tasks'
del(task['static'])
include_data = task['include']
del(task['include'])
tokens = re.findall('[\w.]+|[\"].+?[\"]|{{.+?}}', include_data)
filename = tokens[0]
tokens = tokens[1:]
task[new_include] = filename
task['vars'] = {}
it = iter(tokens)
for token in it:
task['vars'][token] = next(it)
if task['vars'] == {}:
del(task['vars'])
else:
task.move_to_end('vars', last=False)
task.move_to_end(new_include, last=False)
task.move_to_end('name', last=False)
out = yaml.dump(tasks, Dumper=AnsibleDumper)
print(out)
# write back to original file
#ansibleFile.seek(0)
#ansibleFile.write(out)
#ansibleFile.truncate()