This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
configure_netbox.py
459 lines (407 loc) · 16 KB
/
configure_netbox.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
###################################################
# This script takes the variables defined in the file variables.yml and make rest calls to Netbox to configure it.
###################################################
###################################################
# usage: python configure_netbox.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['ip']
my_variables_file.close()
return my_variables_in_yaml
def create_device_roles():
url=url_base + 'api/dcim/device-roles/'
for item in my_variables_in_yaml['device-roles']:
payload={
"name": item,
"slug": item,
"color": "2196f3"
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
if rest_call.status_code == 201:
print 'device role ' + item + ' successfully created'
else:
print 'failed to create device role ' + item
def get_device_role_id(role):
url=url_base + 'api/dcim/device-roles/?name=' + role
rest_call = requests.get(url, headers=headers)
#pprint (rest_call.json())
#if rest_call.status_code != 200:
# print 'failed to get the id of the role ' + role
role_id = rest_call.json()['results'][0]['id']
#print role_id
return role_id
def create_tenants():
url=url_base + 'api/tenancy/tenants/'
for item in my_variables_in_yaml['tenants']:
payload={
"name": item,
"slug": item
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
if rest_call.status_code == 201:
print 'tenant ' + item + ' successfully created'
else:
print 'failed to create tenant ' + item
def get_tenant_id(tenant):
url=url_base + 'api/tenancy/tenants/?name=' + tenant
rest_call = requests.get(url, headers=headers)
#pprint (rest_call.json())
#if rest_call.status_code != 200:
# print 'failed to get the id of the tenant ' + tenant
tenant_id = rest_call.json()['results'][0]['id']
#print tenant_id
return tenant_id
def get_manufacturer_id(manufacturer):
url=url_base + 'api/dcim/manufacturers/?name=' + manufacturer
rest_call = requests.get(url, headers=headers)
#pprint (rest_call.json())
#if rest_call.status_code != 200:
# print 'failed to get the id of the manufacturer ' + manufacturer
manufacturer_id = rest_call.json()['results'][0]['id']
#print manufacturer_id
return manufacturer_id
def get_platform_id(platform):
url=url_base + 'api/dcim/platforms/?name=' + platform
rest_call = requests.get(url, headers=headers)
#pprint (rest_call.json())
#if rest_call.status_code != 200:
# print 'failed to get the id of the platform ' + platform
platform_id = rest_call.json()['results'][0]['id']
#print platform_id
return platform_id
def create_platform():
url=url_base + 'api/dcim/platforms/'
payload={
"napalm_driver": "junos",
'name': 'junos',
'rpc_client': 'juniper-junos',
'slug': 'junos'
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
if rest_call.status_code == 201:
print 'platform junos with a junos napalm_driver successfully created'
else:
print 'failed to create platform junos'
def create_sites():
url=url_base + 'api/dcim/sites/'
tenant_id=get_tenant_id(my_variables_in_yaml['tenants'][0])
for item in my_variables_in_yaml['sites']:
payload={
"name": item,
"slug": item,
"tenant": tenant_id
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
if rest_call.status_code == 201:
print 'site ' + item + ' successfully created'
else:
print 'failed to create site ' + item
def get_site_id(site):
url=url_base + 'api/dcim/sites/?name=' + site
rest_call = requests.get(url, headers=headers)
#pprint (rest_call.json())
#if rest_call.status_code != 200:
# print 'failed to get the id of the site ' + site
site_id = rest_call.json()['results'][0]['id']
#print site_id
return site_id
def create_device_types():
url=url_base + 'api/dcim/device-types/'
Juniper_id=get_manufacturer_id('Juniper')
device_types_list=[{"manufacturer": Juniper_id, "model": "qfx5100-48s-6q", "slug": "qfx5100-48s-6q", "part_number": "650-049938", "u_height": 1, "is_full_depth": True, "is_network_device": True},{ "manufacturer": Juniper_id, "model": "qfx10002-36q", "slug": "qfx10002-36q", "part_number": "750-059497", "u_height": 2, "is_full_depth": True, "is_network_device": True}]
for item in device_types_list:
payload={
"manufacturer": item['manufacturer'],
"model": item['model'],
"slug": item['slug'],
"part_number": item['part_number'],
"u_height": item['u_height'],
"is_full_depth": item ["is_full_depth"],
"is_network_device": item["is_network_device"]
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
if rest_call.status_code == 201:
print 'device type ' + item['model'] + ' successfully created'
else:
print 'failed to create device type ' + item['model']
def get_device_type_id(model):
url=url_base + 'api/dcim/device-types/?model=' + model
rest_call = requests.get(url, headers=headers)
#pprint (rest_call.json())
#if rest_call.status_code != 200:
# print 'failed to get the id of the device_type ' + model
device_type_id = rest_call.json()['results'][0]['id']
#print device_type_id
return device_type_id
def create_interface_templates_for_qfx5100_48s_6q():
url=url_base + 'api/dcim/interface-templates/'
for item in range (0, 48):
payload={
"device_type": get_device_type_id('qfx5100-48s-6q'),
"name": "xe-0/0/" + str(item),
"form_factor": 1200,
"mgmt_only": False
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
for item in range (48, 54):
payload={
"device_type": get_device_type_id('qfx5100-48s-6q'),
"name": "et-0/0/" + str(item),
"form_factor": 1400,
"mgmt_only": False
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
payload={
"device_type": get_device_type_id('qfx5100-48s-6q'),
"name": "vme0",
"form_factor": 1000,
"mgmt_only": True
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
def create_power_port_templates(model):
url=url_base + 'api/dcim/power-port-templates/'
for item in ['Power Supply 0','Power Supply 1']:
payload={
"device_type": get_device_type_id(model),
"name": item
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
def create_interface_templates_for_qfx10002_36q():
url=url_base + 'api/dcim/interface-templates/'
for item in range (0, 36):
payload={
"device_type": get_device_type_id('qfx10002-36q'),
"name": "et-0/0/" + str(item),
"form_factor": 1400,
"mgmt_only": False
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
payload={
"device_type": get_device_type_id('qfx10002-36q'),
"name": "em0",
"form_factor": 1000,
"mgmt_only": True
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
def create_prefix_roles():
url=url_base + 'api/ipam/roles/'
for item in my_variables_in_yaml['prefix_roles']:
payload={
"name": item,
"slug": item
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
if rest_call.status_code == 201:
print 'prefix role ' + item + ' successfully created'
else:
print 'failed to create prefix role ' + item
def get_prefix_role_id(prefix_role):
url=url_base + 'api/ipam/roles/?name=' + prefix_role
rest_call = requests.get(url, headers=headers)
#pprint (rest_call.json())
#if rest_call.status_code != 200:
# print 'failed to get the id of the prefix_role ' + prefix_role
prefix_role_id = rest_call.json()['results'][0]['id']
#print prefix_role_id
return prefix_role_id
def create_prefixes():
url=url_base + 'api/ipam/prefixes/'
for item in my_variables_in_yaml['prefixes']:
payload={
"prefix": item['prefix'],
"tenant": get_tenant_id(my_variables_in_yaml['tenants'][0]),
"status": 1,
"role": get_prefix_role_id(item['role'])
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
if rest_call.status_code == 201:
print 'prefix ' + item['prefix'] + ' successfully created'
else:
print 'failed to create prefix ' + item['prefix']
def create_devices():
url=url_base + 'api/dcim/devices/'
for item in my_variables_in_yaml['devices']:
payload={
"name": item['name'],
"device_type": get_device_type_id(item['device_type']),
"status": 1,
"device_role": get_device_role_id(item['device_role']),
"platform": get_platform_id('junos'),
"site": get_site_id(item['site']),
"tenant": get_tenant_id(my_variables_in_yaml['tenants'][0])
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
#pprint (rest_call.json())
if rest_call.status_code == 201:
print 'device ' + item['name'] + ' successfully created'
else:
print 'failed to create device ' + item['name']
def get_device_details(device):
device_id=get_device_id(device)
url=url_base + 'api/dcim/devices/' + str(device_id)
rest_call = requests.get(url, headers=headers)
# print url
# pprint(rest_call.json())
name=rest_call.json()['name']
device_type=rest_call.json()['device_type']['id']
status=rest_call.json()['status']['value']
device_role=rest_call.json()['device_role']['id']
platform=rest_call.json()['platform']['id']
site=rest_call.json()['site']['id']
tenant=rest_call.json()['tenant']['id']
id=rest_call.json()['id']
payload={
"name": name,
"device_type": device_type,
"status": status,
"device_role": device_role,
"platform": platform,
"site": site,
"tenant": tenant
}
#print payload
return payload
def create_ip_addresses(addresses):
url=url_base + 'api/ipam/ip-addresses/'
for item in my_variables_in_yaml[addresses]:
device=item['device']
interface=item['interface']
payload={
"status": 1,
"address": item['ip'],
"tenant": get_tenant_id(my_variables_in_yaml['tenants'][0]),
"interface": get_interface_id(interface, device)
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
if rest_call.status_code == 201:
print 'address ip ' + item['ip'] + ' successfully created'
else:
print 'failed to create ip address ' + item['ip']
def enable_management_ip_address():
for item in my_variables_in_yaml['management_addresses']:
if item['mgmt_only']==True:
device_id=get_device_id(item['device'])
interface_id=get_interface_id(item['interface'], item['device'])
address_id=get_address_id((item['device']), interface_id)
url=url_base + 'api/dcim/devices/' + str(device_id) + '/'
payload={
"primary_ip4": address_id
}
rest_call = requests.patch(url, headers=headers, data=json.dumps(payload))
def get_address_id(device, interface_id):
url=url_base + 'api/ipam/ip-addresses/?device=' + device + '&interface_id=' + str(interface_id)
rest_call = requests.get(url, headers=headers)
#pprint (rest_call.json())
#if rest_call.status_code != 200:
# print 'failed to get the address id for device ' + device + ' and interface_id ' + interface_id
address_id = rest_call.json()['results'][0]['id']
#print "address_id is " + str(address_id)
return address_id
def get_device_id(device):
url=url_base + 'api/dcim/devices/?name=' + device
rest_call = requests.get(url, headers=headers)
#pprint (rest_call.json())
#if rest_call.status_code != 200:
# print 'failed to get the id of the device ' + device
device_id = rest_call.json()['results'][0]['id']
#print "device_id is " + str(device_id)
return device_id
def get_interface_id(interface, device):
url=url_base + 'api/dcim/interfaces/?name=' + interface + '&device=' + device
rest_call = requests.get(url, headers=headers)
#pprint (rest_call.json())
#if rest_call.status_code != 200:
# print 'failed to get the id of the interface ' + interface + ' of the device ' + device
interface_id = rest_call.json()['results'][0]['id']
#print "interface_id is " + str(interface_id)
return interface_id
def create_interface_connections():
url=url_base + 'api/dcim/interface-connections/'
for item in my_variables_in_yaml['interface_connections']:
device_a=item['device_a']
device_b=item['device_b']
interface_a=item['interface_a']
interface_b=item['interface_b']
interface_a_id = get_interface_id(interface_a, device_a)
interface_b_id = get_interface_id(interface_b, device_b)
payload={
"connection_status": True,
"interface_b": interface_b_id,
"interface_a": interface_a_id
}
rest_call = requests.post(url, headers=headers, data=json.dumps(payload))
if rest_call.status_code == 201:
print 'interface connection between ' + item['device_a'] + ' ' + item['interface_a'] + ' and ' + item['device_b'] + ' ' + item['interface_b'] + ' successfully created'
else:
print 'failed to create interface connection ' + item
######################################################
# this block is the Netbox configuration using REST calls
######################################################
my_variables_in_yaml=import_variables_from_file()
url_base = 'http://' + my_variables_in_yaml['ip'] + '/'
token = my_variables_in_yaml['token']
headers={
'Authorization': 'Token ' + token,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
create_device_roles()
#for item in my_variables_in_yaml['device-roles']:
# get_device_role_id(item)
create_tenants()
#get_tenant_id(my_variables_in_yaml['tenants'][0])
create_sites()
#for item in my_variables_in_yaml['sites']:
# get_site_id(item)
create_device_types()
#get_device_type_id('qfx10002-36q')
#get_device_type_id('qfx5100-48s-6q')
#for item in ['qfx5100-48s-6q','qfx10002-36q']:
# get_device_type_id(item)
create_interface_templates_for_qfx5100_48s_6q()
create_interface_templates_for_qfx10002_36q()
for item in ['qfx5100-48s-6q','qfx10002-36q']:
create_power_port_templates(item)
create_prefix_roles()
for item in my_variables_in_yaml['prefix_roles']:
get_prefix_role_id(item)
create_prefixes()
#get_platform_id('Juniper Junos')
#get_platform_id('junos')
create_platform()
create_devices()
create_ip_addresses('management_addresses')
enable_management_ip_address()
#get_device_details('QFX5100-183')
create_ip_addresses('ip_addresses')
create_interface_connections()