This repository has been archived by the owner on Oct 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconfigure_awx.py
289 lines (252 loc) · 11 KB
/
configure_awx.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
###################################################
# This script takes the variables defined in the file variables.yml and make rest calls to AWX to configure it.
###################################################
###################################################
# usage: python configure_awx.py
###################################################
###################################################
# This block indicates the various imports
###################################################
import requests
from requests.auth import HTTPBasicAuth
import json
from pprint import pprint
import yaml
import time
##################################################
# This block defines the functions we will use
###################################################
def import_variables_from_file():
my_variables_file=open('variables.yml', 'r')
my_variables_in_string=my_variables_file.read()
# print my_variables_in_string
my_variables_in_yaml=yaml.load(my_variables_in_string)
# print my_variables_in_yaml
# print my_variables_in_yaml['awx']['ip']
my_variables_file.close()
return my_variables_in_yaml
def create_the_organization():
url=url_base + '/api/v2/organizations/'
payload = {
"name": my_variables_in_yaml['organization']['name']
}
rest_call = requests.post(url, headers=headers, auth=(authuser, authpwd), data=json.dumps(payload))
# pprint (rest_call.json())
if rest_call.status_code == 201:
print my_variables_in_yaml['organization']['name'] + ' organization successfully created'
else:
print 'failed to create the organization ' + my_variables_in_yaml['organization']['name']
def get_the_id_of_the_organization():
url = url_base + "/api/v2/organizations/"
rest_call = requests.get(url, headers=headers, auth=(authuser, authpwd))
# pprint (rest_call.json())
for item in rest_call.json()['results']:
if item['name'] == my_variables_in_yaml['organization']['name']:
# print my_variables_in_yaml['organization']['name'] + " organization id is " + str(item['id'])
organization_id = str(item['id'])
return organization_id
def create_the_team():
url = url_base + '/api/v2/organizations/' + organization_id + '/teams/'
payload = {
"name": my_variables_in_yaml['team']['name'],
"organization": organization_id
}
rest_call = requests.post(url, headers=headers, auth=(authuser, authpwd), data=json.dumps(payload))
# pprint (rest_call.json())
if rest_call.status_code == 201:
print my_variables_in_yaml['team']['name'] + ' team successfully created and added to the ' + my_variables_in_yaml['organization']['name'] + ' organization'
else:
print 'failed to create the team ' + my_variables_in_yaml['team']['name'] + ' in the organization ' + my_variables_in_yaml['organization']['name']
def get_the_team_id():
url = url_base + "/api/v2/teams/"
rest_call = requests.get(url, headers=headers, auth=(authuser, authpwd))
# pprint (rest_call.json())
for item in rest_call.json()['results']:
if item['name'] == my_variables_in_yaml['team']['name']:
# print "team id is " + str(item['id'])
team_id = str(item['id'])
return team_id
def add_the_user_to_the_team():
url = url_base + '/api/v2/teams/' + team_id + '/users/'
payload = {
"username": my_variables_in_yaml['user']['username'],
"first_name": my_variables_in_yaml['user']['first_name'],
"last_name": my_variables_in_yaml['user']['last_name'],
"is_superuser": True,
"password": my_variables_in_yaml['user']['password']
}
rest_call = requests.post(url, headers=headers, auth=(authuser, authpwd), data=json.dumps(payload))
# pprint (rest_call.json())
if rest_call.status_code == 201:
print my_variables_in_yaml['user']['username'] + ' user successfully created and added to the ' + my_variables_in_yaml['team']['name'] + ' team'
else:
print 'failed to create the user ' + my_variables_in_yaml['user']['username'] + ' in the team ' + my_variables_in_yaml['team']['name']
def add_the_user_to_the_organization():
url = url_base + '/api/v2/organizations/' + organization_id + '/users/'
payload = {
"username": my_variables_in_yaml['user']['username'],
"first_name": my_variables_in_yaml['user']['first_name'],
"last_name": my_variables_in_yaml['user']['last_name'],
"is_superuser": True,
"password": my_variables_in_yaml['user']['password']
}
rest_call = requests.post(url, headers=headers, auth=(authuser, authpwd), data=json.dumps(payload))
# pprint (rest_call.json())
if rest_call.status_code == 201:
print my_variables_in_yaml['user']['username'] + ' user successfully created and added to the ' + my_variables_in_yaml['organization']['name'] + ' organization'
else:
print 'failed to create the user ' + my_variables_in_yaml['user']['username'] + ' in the organization ' + my_variables_in_yaml['organization']['name']
def add_the_project_to_the_organization():
url = url_base + '/api/v2/projects/'
payload = {
"name": my_variables_in_yaml['project']['name'],
"scm_type": "git",
"scm_url": my_variables_in_yaml['project']['git_url'],
"scm_branch": "",
"scm_clean": True,
"scm_delete_on_update": True,
"organization": int(organization_id),
"scm_update_on_launch": False
}
rest_call = requests.post(url, headers=headers, auth=(authuser, authpwd), data=json.dumps(payload))
# pprint (rest_call.json())
if rest_call.status_code == 201:
print my_variables_in_yaml['project']['name'] + ' project successfully created and added to the ' + my_variables_in_yaml['organization']['name'] + ' organization'
else:
print 'failed to create the project ' + my_variables_in_yaml['project']['name'] + ' in the organization ' + my_variables_in_yaml['organization']['name']
def get_the_project_id():
url = url_base + "/api/v2/projects/"
rest_call = requests.get(url, headers=headers, auth=(authuser, authpwd))
# pprint (rest_call.json())
for item in rest_call.json()['results']:
if item['name'] == my_variables_in_yaml['project']['name']:
# print "Project id is " + str(item['id'])
project_id = str(item['id'])
return project_id
def add_credentials():
url = url_base + '/api/v2/credentials/'
payload = {
"name": my_variables_in_yaml['credentials']['name'],
"organization": organization_id,
"credential_type": 1,
"inputs": {
"username": my_variables_in_yaml['credentials']['username'],
"password": my_variables_in_yaml['credentials']['password']
}
}
rest_call = requests.post(url, headers=headers, auth=(authuser, authpwd), data=json.dumps(payload))
# pprint (rest_call.json())
if rest_call.status_code == 201:
print my_variables_in_yaml['credentials']['name'] + ' credentials successfully created and added to the ' + my_variables_in_yaml['organization']['name'] + ' organization'
else:
print 'failed to create the credentials ' + my_variables_in_yaml['credentials']['name'] + ' in the organization ' + my_variables_in_yaml['organization']['name']
def get_the_credential_id():
url = url_base + "/api/v2/credentials/"
rest_call = requests.get(url, headers=headers, auth=(authuser, authpwd))
# pprint (rest_call.json())
for item in rest_call.json()['results']:
if item['name'] == my_variables_in_yaml['credentials']['name']:
# print "inventory id is " + str(item['id'])
credential_id = str(item['id'])
return credential_id
def add_inventory():
url = url_base + '/api/v2/inventories/'
payload = {
"name": my_variables_in_yaml['inventory']['name'],
"organization": organization_id,
"has_inventory_sources": True
}
rest_call = requests.post(url, headers=headers, auth=(authuser, authpwd), data=json.dumps(payload))
# pprint (rest_call.json())
if rest_call.status_code == 201:
print my_variables_in_yaml['inventory']['name'] + ' inventory successfully created and added to the ' + my_variables_in_yaml['organization']['name'] + ' organization'
else:
print 'failed to create the inventory ' + my_variables_in_yaml['credentials']['name'] + ' in the organization ' + my_variables_in_yaml['organization']['name']
def get_the_inventory_id():
url = url_base + "/api/v2/inventories/"
rest_call = requests.get(url, headers=headers, auth=(authuser, authpwd))
# pprint (rest_call.json())
for item in rest_call.json()['results']:
if item['name'] == my_variables_in_yaml['inventory']['name']:
# print "inventory id is " + str(item['id'])
inventory_id = str(item['id'])
return inventory_id
def add_inventory_source():
url = url_base + '/api/v2/inventories/' + inventory_id + '/inventory_sources/'
payload = {
"source_path": my_variables_in_yaml['inventory']['file'],
"source_project": project_id,
"name": "inventory_source",
"overwrite_vars": True,
"update_on_launch": True,
"source": "scm"
}
rest_call = requests.post(url, headers=headers, auth=(authuser, authpwd), data=json.dumps(payload))
# pprint (rest_call.json())
if rest_call.status_code == 201:
print my_variables_in_yaml['inventory']['file'] + ' file successfully added as a source to ' + my_variables_in_yaml['inventory']['name'] + ' inventory'
else:
print 'failed to add the file ' + my_variables_in_yaml['inventory']['name'] + ' as a source of the inventory ' + my_variables_in_yaml['inventory']['name']
def add_templates():
url = url_base + '/api/v2/job_templates/'
for item in my_variables_in_yaml['playbooks']:
time.sleep(1)
payload = {
'name': "run_" + item,
'description': "template to execute " + item + " playbook",
'job_type': 'run',
'inventory': inventory_id,
'project': project_id,
'playbook': item,
'credential': credential_id,
'verbosity': 0,
"extra_vars": '',
"skip_tags": '',
"start_at_task": '',
"ask_limit_on_launch": True,
"ask_variables_on_launch": False,
"ask_verbosity_on_launch": True
}
rest_call = requests.post(url, headers=headers, auth=(authuser, authpwd), data=json.dumps(payload))
# pprint (rest_call.json())
if rest_call.status_code == 201:
print 'run_' + item + ' template successfully created ' + 'using the playbook ' + item
else:
print 'failed to create the template run_' + item + ' using the playbook ' + item
######################################################
# this block is the AWX configuration using REST calls
######################################################
my_variables_in_yaml=import_variables_from_file()
# this is the default AWX user
authuser = 'admin'
authpwd = 'password'
headers = { 'content-type' : 'application/json' }
url_base = 'http://' + my_variables_in_yaml['awx']['ip']
# print url_base
create_the_organization()
time.sleep(2)
organization_id = get_the_id_of_the_organization()
time.sleep(2)
create_the_team()
time.sleep(2)
team_id = get_the_team_id()
time.sleep(2)
add_the_user_to_the_organization()
# add_the_user_to_the_team()
time.sleep(2)
add_the_project_to_the_organization()
time.sleep(2)
project_id = get_the_project_id()
time.sleep(2)
add_credentials()
time.sleep(2)
credential_id = get_the_credential_id()
time.sleep(2)
add_inventory()
time.sleep(2)
inventory_id = get_the_inventory_id()
time.sleep(2)
add_inventory_source()
print 'wait 30 seconds before to resume'
time.sleep(30)
add_templates()