-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsat5cfg2ansible.py
executable file
·195 lines (162 loc) · 7.15 KB
/
sat5cfg2ansible.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
import json
import yaml
from jinja2 import Template
import os
import sys
import magic
import csv
VERBOSE = False
INPUT_DIR = sys.argv[1]
OUTPUT_DIR = INPUT_DIR + "/" + "transformed_files"
WORKDIR = OUTPUT_DIR + "/roles"
ROLE_AUTHOR_NAME = "<change me>"
all_files = []
json_files = []
try:
all_files = os.listdir(INPUT_DIR)
except FileNotFoundError as e:
print(e)
for file in all_files:
if os.path.isfile(INPUT_DIR + "/" + file) and file.endswith(".json"):
json_files.append(file)
if not json_files:
print("No json files found")
for file in json_files:
# strip json from filename
AnsibleRoleName = os.path.splitext(file)[0]
if VERBOSE:
print("Creating role: {} with file: {} as input".format(AnsibleRoleName,file))
RoleDirectory = WORKDIR + '/' + AnsibleRoleName
PlaybookDirectory = OUTPUT_DIR + '/playbooks'
for d in RoleDirectory, PlaybookDirectory:
try:
if not os.path.exists(d):
os.makedirs(d)
except OSError as e:
print(e)
# Create ansible directories for role
DirsToCreate = ["tasks","files","meta"]
for d in DirsToCreate:
try:
to_create = RoleDirectory + '/' + d
if not os.path.exists(to_create):
os.makedirs(to_create)
except OSError as e:
print(e)
with open(INPUT_DIR + "/" + file, 'r') as json_file:
data = json.load(json_file)
# Create meta.yml
ROLE_DESCRIPTION = data[0]['description'].replace("\n", " ")
with open('templates/meta.j2') as f:
tmpl = Template(f.read())
meta_tmpl_processed = tmpl.render(role_author_name = ROLE_AUTHOR_NAME, role_description = ROLE_DESCRIPTION)
AnsibleFileToWrite = RoleDirectory + "/meta/main.yml"
with open(AnsibleFileToWrite, 'w') as yaml_file:
yaml_file.write(meta_tmpl_processed)
# Create playbook
with open('templates/playbook.j2') as f:
tmpl = Template(f.read())
playbook_tmpl_processed = tmpl.render(role_name = AnsibleRoleName, role_description = ROLE_DESCRIPTION)
AnsibleFileToWrite = PlaybookDirectory + "/" + AnsibleRoleName + ".yml"
with open(AnsibleFileToWrite, 'w') as yaml_file:
yaml_file.write(playbook_tmpl_processed)
# Create README
with open('templates/README.j2') as f:
tmpl = Template(f.read())
readme_tmpl_processed = tmpl.render(role_name = AnsibleRoleName, role_description = ROLE_DESCRIPTION)
AnsibleFileToWrite = RoleDirectory + "/README.md"
with open(AnsibleFileToWrite, 'w') as yaml_file:
yaml_file.write(readme_tmpl_processed)
for i in data[0]['files']:
# DIRECTORY ACTIONS
if i['type'] == 'directory':
DIRECTORY_DESTINATION = i['path']
TASK_DESCRIPTION = "Create directory " + DIRECTORY_DESTINATION
DIRECTORY_MODE = i['permissions']
DIRECTORY_OWNER = i['owner']
DIRECTORY_GROUP = i['group']
create_directory_template = [
{
"name": TASK_DESCRIPTION,
"ansible.builtin.file": {
"path": DIRECTORY_DESTINATION,
"state": 'directory',
"mode": DIRECTORY_MODE,
"owner": DIRECTORY_OWNER,
"group": DIRECTORY_GROUP,
},
"tags": [
"directories"
]
}
]
AnsibleFileToWrite = RoleDirectory + "/tasks/main.yml"
with open(AnsibleFileToWrite, 'a') as yaml_file:
configuration = yaml.dump(create_directory_template, yaml_file, sort_keys=False)
# cvs file
CSV_FILE_PATH = RoleDirectory + "/" + AnsibleRoleName + "_checklist.csv"
csv_file = open(CSV_FILE_PATH, 'w', newline='')
writer = csv.writer(csv_file, delimiter=';')
writer.writerow(["Configuration Channel", "File Path", "File Name", "Type", "Status", "Comment", "Duplicate", "Satellite Variable", "Sensitive Data"])
for i in data[0]['files']:
# FILE ACTIONS
if i['type'] == 'file':
dirname = os.path.dirname(i['path'])
basename = os.path.basename(i['path'])
FILE_CONTENT = i['contents'].encode('ascii', 'ignore').decode('ascii')
FileToWrite = RoleDirectory + "/files/" + basename
magic_config = magic.Magic(uncompress=False)
magic_filetype_found = magic_config.from_buffer(FILE_CONTENT)
magic_filetype_fixed = magic_filetype_found.replace(';', ' |')
writer.writerow([AnsibleRoleName, dirname, basename, magic_filetype_fixed, "", "", "", "", ""])
with open(FileToWrite, "w") as f:
f.write(FILE_CONTENT)
FILE_NAME = basename
TASK_DESCRIPTION = "Copy file " + FILE_NAME
FILE_DESTINATION = i['path']
FILE_MODE = i['permissions']
FILE_OWNER = i['owner']
FILE_GROUP = i['group']
copy_file_template = [
{
"name": TASK_DESCRIPTION,
"ansible.builtin.copy": {
"src": FILE_NAME,
"dest": FILE_DESTINATION,
"mode": FILE_MODE,
"owner": FILE_OWNER,
"group": FILE_GROUP,
"backup": True
},
"tags": [
"files"
]
}
]
AnsibleFileToWrite = RoleDirectory + "/tasks/main.yml"
with open(AnsibleFileToWrite, 'a') as yaml_file:
configuration = yaml.dump(copy_file_template, yaml_file, sort_keys=False)
csv_file.close()
for i in data[0]['files']:
# SYMLINK ACTIONS
if i['type'] == 'symlink':
SYMLINK_SOURCE = i['path']
SYMLINK_DESTINATION = i['target_path']
TASK_DESCRIPTION = "Create directory " + SYMLINK_DESTINATION
create_symlink_template = [
{
"name": TASK_DESCRIPTION,
"ansible.builtin.file": {
"src": SYMLINK_SOURCE,
"dest": SYMLINK_DESTINATION,
"state": 'link'
},
"tags": [
"symlinks"
]
}
]
AnsibleFileToWrite = RoleDirectory + "/tasks/main.yml"
with open(AnsibleFileToWrite, 'a') as yaml_file:
configuration = yaml.dump(create_symlink_template, yaml_file, sort_keys=False)