← Back to list

Implementation of the Data Model on Netbox: A Source of Truth using Python.

I came across the “word” source of truth when I first started out as a NetOps engineer. Next, it was the entrance to hell! The market is…

NetOpsChic · 2024-07-16 17:26 · 5 claps · 3.0 min read
#netbox-global #python #api #data-model
Open on Medium ↗
Wiki topics: ECO · Economy · General

Implementation of the Data Model on Netbox: A Source of Truth using Python.

I came across the “word” source of truth when I first started out as a NetOps engineer. Next, it was the entrance to hell! The market is filled with incredible tools that can serve as excellent SoT for your network. BUT the OG SoT, as I like to refer to it, is Netbox.

What is SoT?

Let’s start with it. First things first. What is SoT? I don’t feel like delving into the definition. I prefer to think of it as a database for the network infrastructure. A driving factor behind network infrastructure. An ultimate source of truth would reflect any modifications implemented in it for all end nodes. For example, Router x IP is updated in Netbox, which should trigger the IP change in that specific router in the network as well!

As I always encourage my budding NetOps engineers to get their hands dirty with local implementation, this blog will cover installing Netbox and creating a very basic data model in it.

To install Netbox, follow their official website, which has extremely explicit installation instructions. https://netboxlabs.com › netbox › stable › installation

After the installation on your local machine, the netbox can be accessed at http://localhost:8000/

Netbox v4

Netbox v4

What is Data Model?

A data model is a structure that describes how data looks in your system/infrastructure. In this example, we have a data center (site) with two network devices: a Cisco (devices) router and a switch (device type) from Cisco (manufacturer).

Python Libraries

The library used for connecting to a NetBox instance via API is pynetbox.

pip install pynetbox

Usage:

import pynetbox
nb = pynetbox.api(
    'http://localhost:8000',
    token=''
)

A token can be obtained under Users in the local netbox instance.

Create a directory and a python file called data-model.py and conifg.py for all the variables, like URL and TOKEN


import config
import pynetbox

# Connect to NetBox
nb = pynetbox.api(config.NETBOX_URL, token=config.API_TOKEN)

def create_site(name, slug):
    site = nb.dcim.sites.create(name=name, slug=slug)
    return site

def create_manufacturer(name, slug):
    manufacturer = nb.dcim.manufacturers.create(name=name, slug=slug)
    return manufacturer

def create_device_type(model, slug, manufacturer_id):
    device_type = nb.dcim.device_types.create(model=model, slug=slug, manufacturer=manufacturer_id)
    return device_type

def create_device_role(name, slug):
    device_role = nb.dcim.device_roles.create(name=name, slug=slug)
    return device_role

def create_device(name, device_type_id, device_role_id, site_id):
    device = nb.dcim.devices.create(
        name=name,
        device_type=device_type_id,
        role=device_role_id,
        site=site_id
    )
    return device

if __name__ == "__main__":
    site = create_site("Data Center", "data-center")
    print("Created site:", site)

    manufacturer = create_manufacturer("Cisco", "cisco")
    print("Created manufacturer:", manufacturer)

    device_type = create_device_type("Cisco XXX", "cisco-xxx", manufacturer['id'])
    print("Created device type:", device_type)

    device_role = create_device_role("Switch", "switch")
    print("Created device role:", device_role)

    device = create_device("Switch-1", device_type['id'], device_role['id'], site['id'])
    print("Created device:", device)

It is not necessary to know how to code; one can create syntax using an AI tool or find it online. Understanding what’s going on and using the appropriate cues are crucial. It is not necessary to haul out a large book on “how to code” just because you want to be a network automation engineer. In my opinion, the foundations are enough, and the ability to use AI tools correctly important.

The code basically creates a separate function to create each component on the data model, like a site, device, device role, manufacturer, etc., and calls the function.

After every variable and secret is in place, fire away the script!

python3 data-model.py

If everything is in place, the output will look like this:

and the netbox should be populated with your data model.

Take the plunge and give it a shot. You’re your own best mentor. Remember, this is just the beginning, not the finale. Stay tuned for my next blog, where I’ll swap out the Python script for a more user-friendly tool called Ansible.

For codes refer https://github.com/NetOpsChic/netbox-python


메타데이터
post_id
3d676df027fe
slug
implementation-of-the-data-model-on-netbox-a-source-of-truth-using-python-3d676df027fe
url
https://medium.com/@netopschic/implementation-of-the-data-model-on-netbox-a-source-of-truth-using-python-3d676df027fe
canonical_url
https://medium.com/@netopschic/implementation-of-the-data-model-on-netbox-a-source-of-truth-using-python-3d676df027fe
author_url
https://medium.com/@netopschic
status
ok
fetched_at
2026-08-20 16:43:42