NetBox Setup and Basic Automation

For this post, I wanted to just go through the NetBox setup and the ability to get started with automation.

Docker Compose

I will be using Docker for NetBox. There is an easy to get started version of NetBox using NetBox Docker

Install Docker

Download the Docker Compose binary from the official GitHub repo

Apply executable permissions to the Docker Compose binary

Verify Install

The commands for docker compose installed for me are docker-compose. The documentation for NetBox are docker compose. More on this in the NetBox install section.

NetBox Setup

As mentioned in the Docker Compose section of this post, I am going to be using NetBox Docker. This is the easiest way I think to get it working in a lab. On the GitHub repo there is a lot of information. The minimum required is to have Docker Compose installed, and to then run the following. Please note that my Docker Compose install is using docker-compose as the syntax and not docker compose

To create an admin user, use the following command. This will ask you what you want the credentials to be.

That covers it, if you want the docker container to run in the background, use the -d flag to detach it.

Setting Up API Token

To setup the API token, go to the web interface http://127.0.0.1:8000/, login with the credentials created in the setup and click the top right user icon and select the Admin cog.

Once in here, this is the Django admin interface. Navigate to Users and Tokens.

Next click Add Token,

Select the user to create the token for. I only have admin available. Click save at the bottom and a token will be generated as shown in the screenshot above. Click on the key to go and copy it.

Adding Sites to NetBox

Now the API token has been setup we can almost start using the NetBox API. However, there isn’t anything in NetBox, so to go and manually a few things to NetBox, so we can search them with the API.

I have created the following in NetBox. This was very repetitive, easy to confuse and make mistakes during the process. But it will be automated soon.

  • 3 Sites
  • 2 VRFs per site
  • 4 VLANs per site
  • 4 Prefixes (Networks)

Getting Information from NetBox with PyNetBox

This basic script will output the site IDs and names of each site in NetBox

The next script I have created is to get all VLANs in NetBox

The next script is to get all the networks in NetBox.

Adding to NetBox through PyNetBox

This script is to add 10 VLANs to each site from VLAN 100-109.

Using the API Documentation

It’s all well and good, me showing different test scripts that do very little. Pynetbox is a wrapper for the NetBox API. It is simple to use and to see what you want you can use the NetBox API documentation, http://127.0.0.1:8000/api/

To successfully use this, decide what you want to see. Say for example, you want to see all the attributes of the sites. Well they’re under dcim, go into that and then find sites. From here, the full list of attributes is shown for what is already configured in NetBox. Each of these attributes can be called.

How things line up between the API documentation and Pynetbox can be seen with the snippet of code below. The attributes are called are .id, .name and .slug all can be easily seen above in the API documentation.

Leave a Comment

Your email address will not be published. Required fields are marked *