Exporting device inventory from Prime to NetBox part 3: Exporting data to Netbox

Now that we have a csv file with all the input data for Netbox, we can start creating devices.

First, we need to create a device list from the csv file:

def create_device_list(site_abbrev):
    Creates a device list from a csv file
    filename = 'devices_contains-' + site_abbrev + '.csv'
      f = open(filename, "r")
        print(f'{filename} does not exist')
        device_list = []
        for line in f:
          fixed_line = line.replace('\n', '')
        return device_list

The next step is to get a dictionary with all the device properties for a single device from the csv file

def get_properties(device):

     This takes a semicolon-separated list of properties and returns a dictionary of properties for a given device

     device_property_dictionary = {}
     x = device.split(";")

     device_property_dictionary['device_manufacturer'] = x[1]
     device_property_dictionary['device_fullname'] = x[2]
     device_property_dictionary['device_ipaddress'] = x[3]
     device_property_dictionary['device_model'] = x[4]
     device_property_dictionary['device_ostype'] = x[5]
     device_property_dictionary['device_serial'] = x[6]
     device_property_dictionary['device_virtualchassisname'] = x[7]
     device_property_dictionary['device_virtualchassisnumber'] = x[8]
     device_property_dictionary['device_virtualchassispriority'] = x[9]
     device_property_dictionary['device_status']= x[10]

     return device_property_dictionary

Now we can start creating devices.

def create_device(device_list):
    This creates a device in Netbox from a device list
    item_number = 1
    total_devices = str(len(device_list))
    print(f'total number of devices to be added is: {total_devices}')
    for device in device_list:
     device_dict = get_properties(device)
device_function = device_dict['device_function']
     device_fullname = device_dict['device_fullname']
     device_ipaddress = str(device_dict['device_ipaddress'])
     device_model = device_dict['device_model']
     device_manufacturer = device_dict['device_manufacturer']
     device_ostype = device_dict['device_ostype']
     device_serial = device_dict['device_serial']
     device_virtualchassisname = device_dict['device_virtualchassisname']
     vc_position = device_dict['device_virtualchassisnumber']
     vc_priority = device_dict['device_virtualchassispriority']

So far so good. The code will need to be improved later but important thing is that it gets the job done.

Now what we need are ID numbers for each function, manufacturer, device model and platform. These functions etc need to be created in Netbox first (e.g. Manufacturer Cisco or Juniper or HP) and Netbox will create IDs for each object behind the scenes. Now to add a device that has Cisco as its manufacturer, we need to know its id. Therefore we need to retrieve the ID. This is an example function to retrieve the manufacturer id:

def get_manufacturer_id(searched_manufacturer):
            manufacturers = nb.dcim.manufacturers.filter(name=searched_manufacturer)

            for manufacturer in manufacturers:
                manufacturerdict = dict(manufacturer)

                    manufacturerid = str(manufacturerdict.get('id'))
                except Exception as e:
                    return manufacturerid

We proceed in a similar fashion to create functions to retrieve IDs for models (after creating them in Netbox), functions and platforms (=software types like ios or ios-xe).

Now let’s go back to our create_device function:

function_id = get_role_id(device_function)
manufacturer_id = get_manufacturer_id(device_manufacturer)
device_model_id = get_device_model_id(device_model)
platform_id = get_platform_id(device_ostype)

… to be continued…


Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )

Połączenie z %s

%d blogerów lubi to: