How to pull data from Netbox?

I’m using netbox as my inventory, and that seems to work fine. However, I don’t understand how to use the NetBoxInventory2 to pull inventory data from Netbox. I’ve tried looking for documentation on how to use it, but I’m not finding anything. I found a couple use examples from the previous version that no longer work.

import re
from nornir import InitNornir
from nornir.core.filter import F
from nornir_napalm.plugins.tasks import napalm_get
from nornir_utils.plugins.functions import print_result
from nornir_netbox.plugins.inventory import NetBoxInventory2

def main():
nr = InitNornir(config_file=“config.yaml”)
nr = nr.filter(name=“N-Spine1”)

    nb_url, nb_token, ssl_verify = nr.config.inventory.options.values()
    nb_host = re.sub("^.*//|:.*$", "", nb_url)

    NBX = NetBoxInventory2(host=nb_host, port=32768, use_ssl=False, auth_token=nb_token)
    nb_interfaces = NBX.dcim.get_interfaces()

The “NBX.dcim.get_interfaces()” doesn’t appear to be correct anymore. Can anyone explain how this is done now, or link me to any documentation?

Thank you.

You have to create a token and use it in nornir config.yalm file.
From Netbox Gui (UP Right) choose Admin then --> Token create one.
A template config.yalm file:


runners:
plugin: threaded
options:
num_workers: 100
inventory:
plugin: NBInventory
options:
nb_url: ‘http://your-ip:your-port’
nb_token: ‘paste_your_token’
ssl_verify: False

!!! end of config.yalm !!!

Below you may find a script which allow you to run commands on Cisco/Juniper using as inventory the Netbox, we choose the Ventor, the device and give the commands that you want to execute (separated by ,):

from nornir import InitNornir
from nornir_napalm.plugins.tasks import napalm_cli
from nornir_netmiko.tasks import netmiko_send_command
from nornir_utils.plugins.functions import print_result
from nornir.core.filter import F
from nornir.core.task import Task, Result
from netbox import NetBox
import re
import threading
import readline

nr = InitNornir(config_file=’/opt/netbox/netbox/scripts/config.yaml’)
nr.inventory.defaults.username = “xxxxxxx”
nr.inventory.defaults.password = “yyyyyyy”
nb_url, nb_token, ssl_verify = nr.config.inventory.options.values()
nb_host = re.sub("^.//|:.$", “”, nb_url)

interfaces = []
devices = []
hostnames = []
platforms = []
netbox = NetBox (host = “127.0.0.1”, port=8001, use_ssl = False, auth_token = nb_token)

rtr_iosxr = nr.filter(F(platform__contains=“iosxr”) & ~F(model__contains=“ncs”))
rtr_junos = nr.filter(F(platform__contains=“junos”))

def push_configs(task):
config_file = f"configs/{args.initial}/{task.host.name}.txt"
task.run(task=networking.napalm_configure,
replace=False,
filename=config_file,
severity_level=logging.INFO
)

def nornir_send_commands(task, cmds):
task.run(task=napalm_cli,commands=cmds)

def command():
print("\n","!"*3,“You can enter multiple commands separated with ,”,"!"*3,"\n")
list_of_commands = input (“Enter Commands: “)
cmds = list_of_commands.split(”,”)
return cmds

Set an initial value for choice other than the value for ‘quit’.

choice = ‘’

Clear screen

print("\033[H\033[J")

Start a loop that runs until the user enters the value for ‘quit’.

while choice != ‘q’:
print("!"*60)
print("\nFirst you have to choice on which platform you want to execute a command, Cisco or Juniper?")
# Give all the choices in a series of print statements.
print("\n[1] Enter 1 for Cisco")
print("[2] Enter 2 for Juniper")
print("[q] Enter q to quit.")

# Ask for the user's choice.
choice = input("\nPlease enter your choice: ")

# Respond to the user's choice.
if choice == "1":
    print(f'\nYour choice is CISCO -- Below you may find ALL Cisco devices\n')
    rtr_iosxr= nr.filter(F(platform__contains="iosxr") & ~F(model__contains="ncs"))
    cisco_device_dict = rtr_iosxr.dict()['inventory']['hosts'] 
    for key in cisco_device_dict:
        print(cisco_device_dict[key]['name'])
    host = input ("\nPlease chooce ALL or one device: ")
    if host != "ALL":
        rtr_iosxr= nr.filter(F(platform__contains="iosxr") & ~F(model__contains="ncs") & F(name=str(host)))
    cmds=command()
    result = rtr_iosxr.run(task=nornir_send_commands,cmds=cmds)
    print_result(result)
elif choice == "2":
    print(f'\nYour choice is JUNIPER -- Below you may find ALL Juniper devices\n')
    rtr_junos = nr.filter(F(platform__contains="junos"))
    juniper_device_dict = rtr_junos.dict()['inventory']['hosts']
    for key in juniper_device_dict:
        print(juniper_device_dict[key]['name'])
    host = input ("\nPlease chooce ALL or one device: ")
    if host != "ALL":
        rtr_junos = nr.filter(F(platform__contains="junos") & F(name=str(host)))
    cmds=command()
    result = rtr_junos.run(task=nornir_send_commands,cmds=cmds)
    print_result(result)
elif choice == "q":
    print("\nSee you later.\n")
else:
    print("\nI don't understand that choice, please try again.\n")

Print a message that we are all finished.

print(“Thanks again, bye now.”)

BR,
George