forked from JNPRAutomate/netbox_automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_vars_for_jinja_from_netbox.py
59 lines (47 loc) · 1.97 KB
/
generate_vars_for_jinja_from_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
import os
import requests
from requests.auth import HTTPBasicAuth
import json
from pprint import pprint
import yaml
def import_variables_from_file():
my_variables_file=open('variables.yml', 'r')
my_variables_in_string=my_variables_file.read()
my_variables_in_yaml=yaml.load(my_variables_in_string)
my_variables_file.close()
return my_variables_in_yaml
def create_directory(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def get_interface(interface_id):
url = url_base + 'api/dcim/interfaces/' + str(interface_id) + '/'
rest_call = requests.get(url, headers=headers)
interface = rest_call.json()['name']
return interface
create_directory("host_vars")
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'
}
url = url_base + 'api/dcim/devices/?manufacturer=juniper&is_network_device=True&has_primary_ip=True'
rest_call_get_devices = requests.get(url, headers=headers)
for item in rest_call_get_devices.json()['results']:
device_name = item['name']
create_directory('host_vars/' + device_name)
url = url_base + 'api/ipam/ip-addresses/?device=' + device_name
rest_call_get_device_details = requests.get(url, headers=headers)
device_details = []
for item in rest_call_get_device_details.json()['results']:
address = item['address']
interface_id = item['interface']['id']
interface = get_interface(interface_id)
device_details_item = {'interface': str(interface), 'address':str(address)}
device_details.append(device_details_item)
out_file = open("host_vars/" + device_name + "/vars_from_netbox_api.yml", "w")
out_file.write("vars_from_netbox_api:\n")
out_file.write(yaml.dump(device_details, default_flow_style=False))
out_file.close()