Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions simple-bulk-import/Readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This tool takes a CSV of data in an easily collectible format and breaks it down into individual json files for import into Netbox

./netbox_import_gen.py raw-csv.csv

An example csv file is provided here
104 changes: 104 additions & 0 deletions simple-bulk-import/netbox_import_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/python3

import csv
import sys
import json

rawcsvpath = sys.argv[1]

hostdict = csv.DictReader(open(rawcsvpath), delimiter=",")

#create device list
devicetypes = []
devices = []
interfaces = []
ips = []
for importee in hostdict:
print(importee)

# Create Device Type
devicetype = {}
devicetype["manufacturer"] = importee["Manufacturer"]
devicetype["model"] = importee["Device Type"]
devicetype["slud"] = importee["Device Type"]
devicetype["u_height"] = importee["Rack Units"]
devicetypes.append(devicetype)

# Create Device
device = {}
device["tenant"] = importee["Tenant"]
device["device_role"] = importee["Role"]
device["manufacturer"] = importee["Vendor"]
device["device_type"] = importee["Device Type"]
device["status"] = importee["Status"]
device["site"] = importee["Site"]
device["location"] = importee["Location"]
device["rack"] = importee["Rack"]
device["face"] = importee["face"]
device["airflow"] = importee["airflow"]
device["position"] = importee["Rack Position"]
device["name"] = importee["FQDN"]
device["serial"] = importee["Serial"]
device["cf_order_no"] = importee["Purchase Order"]
devices.append(device)

# Create BMC Interface
bmcinterface = {}
bmcinterface["enabled"] = "TRUE"
bmcinterface["mgmt_only"] = "TRUE"
bmcinterface["type"] = "1000base-t"
bmcinterface["name"] = "bmc0"
bmcinterface["device"] = importee["FQDN"]
bmcinterface["vrf"] = importee["BMC VRF"]
bmcinterface["mac_address"] = importee["BMC MAC"]
interfaces.append(bmcinterface)

# Create First Eth interface
ethinterface = {}
ethinterface["enabled"] = "TRUE"
ethinterface["mgmt_only"] = "False"
ethinterface["type"] = importee["NIC 1 Type"]
ethinterface["name"] = importee["NIC 1 Name"]
ethinterface["device"] = importee["FQDN"]
ethinterface["vrf"] = importee["NIC 1 VRF"]
ethinterface["mac_address"] = importee["NIC 1 MAC"]
interfaces.append(ethinterface)

# Create BMC IP
bmcip = {}
bmcip["address"] = importee["BMC IP"]
bmcip["status"] = "active"
bmcip["tenant"] = importee["Tenant"]
bmcip["interface"] = "bmc0"
bmcip["device"] = importee["FQDN"]
ips.append(bmcip)

# Create First Eth IP
ethip = {}
ethip["address"] = importee["NIC 1 IP"]
ethip["status"] = "active"
ethip["tenant"] = importee["Tenant"]
ethip["interface"] = importee["NIC 1 Name"]
ethip["device"] = importee["FQDN"]
ips.append(ethip)

#print(devicetypes)
#print(devices)
#print(interfaces)
#print(ips)

# Output device types json file
with open("devicetypes.json","w", encoding="utf-8") as outfile:
outfile.write(json.dumps(devicetypes))

# Output devices json file
with open("devices.json","w", encoding="utf-8") as outfile:
outfile.write(json.dumps(devices))

# Output Interfaces json file
with open("interfaces.json","w", encoding="utf-8") as outfile:
outfile.write(json.dumps(interfaces))

# Output IPs json file
with open("ips.json","w", encoding="utf-8") as outfile:
outfile.write(json.dumps(ips))
15 changes: 15 additions & 0 deletions simple-bulk-import/raw-csv.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Vendor,Tenant,Status,Role,Location,Site,Rack,Rack Position,Rack Units,Device Type,Manufacturer,face,airflow,NIC 1 Name,NIC 1 Type,NIC 1 MAC,NIC 1 IP,NIC 1 VRF,FQDN,BMC MAC,BMC IP,BMC VRF,Serial,Purchase Order,NIC 2 MAC,password
Lenovo,cloud,staged,,,,MagDB rack419,32,1,hv-2022-lenovo,Lenovo,front,front-back,eth0,25gb-x-sfp28,,,global,hv900.nubes.rl.ac.uk,,10.1.28.1,t1_management,,,,
Lenovo,cloud,staged,,,,MagDB rack419,31,1,hv-2022-lenovo,Lenovo,front,front-back,eth1,100gb-x-qsfp29,,,global,hv901.nubes.rl.ac.uk,,10.1.28.2,t1_management,,,,
Lenovo,cloud,staged,,,,MagDB rack419,30,1,hv-2022-lenovo,Lenovo,front,front-back,eth2,100gb-x-qsfp30,,,global,hv902.nubes.rl.ac.uk,,10.1.28.3,t1_management,,,,
Lenovo,cloud,staged,,,,MagDB rack419,29,1,hv-2022-lenovo,Lenovo,front,front-back,eth3,100gb-x-qsfp31,,,global,hv903.nubes.rl.ac.uk,,10.1.28.4,t1_management,,,,
Lenovo,cloud,staged,,,,MagDB rack419,28,1,hv-2022-lenovo,Lenovo,front,front-back,eth4,100gb-x-qsfp32,,,global,hv904.nubes.rl.ac.uk,,10.1.28.5,t1_management,,,,
Lenovo,cloud,staged,,,,MagDB rack419,27,1,hv-2022-lenovo,Lenovo,front,front-back,eth5,100gb-x-qsfp33,,,global,hv905.nubes.rl.ac.uk,,10.1.28.6,t1_management,,,,
Lenovo,cloud,staged,,,,MagDB rack419,26,1,hv-2022-lenovo,Lenovo,front,front-back,eth6,100gb-x-qsfp34,,,global,hv906.nubes.rl.ac.uk,,10.1.28.7,t1_management,,,,
Lenovo,cloud,staged,,,,MagDB rack419,25,1,hv-2022-lenovo,Lenovo,front,front-back,eth7,100gb-x-qsfp35,,,global,hv907.nubes.rl.ac.uk,,10.1.28.8,t1_management,,,,
Lenovo,cloud,staged,,,,MagDB rack419,24,1,hv-2022-lenovo,Lenovo,front,front-back,eth8,100gb-x-qsfp36,,,global,hv908.nubes.rl.ac.uk,,10.1.28.9,t1_management,,,,
Lenovo,cloud,staged,,,,MagDB rack419,23,1,hv-2022-lenovo,Lenovo,front,front-back,eth9,100gb-x-qsfp37,,,global,hv909.nubes.rl.ac.uk,,10.1.28.10,t1_management,,,,
Lenovo,cloud,staged,,,,MagDB rack419,22,1,hv-2022-lenovo,Lenovo,front,front-back,eth10,100gb-x-qsfp38,,,global,hv910.nubes.rl.ac.uk,,10.1.28.11,t1_management,,,,
Lenovo,cloud,staged,,,,MagDB rack419,21,1,hv-2022-lenovo,Lenovo,front,front-back,eth11,100gb-x-qsfp39,,,global,hv911.nubes.rl.ac.uk,,10.1.28.12,t1_management,,,,
Lenovo,cloud,staged,,,,MagDB rack419,20,1,hv-2022-lenovo,Lenovo,front,front-back,eth12,100gb-x-qsfp40,,,global,hv912.nubes.rl.ac.uk,,10.1.28.13,t1_management,,,,
Lenovo,cloud,staged,,,,MagDB rack419,19,1,hv-2022-lenovo,Lenovo,front,front-back,eth13,100gb-x-qsfp41,,,global,hv913.nubes.rl.ac.uk,,10.1.28.14,t1_management,,,,