Skip to content

Netbox Update Device Interfaces Task¤

task api name: update_device_interfaces

The Netbox Update Device Interfaces Task is a feature of the NorFab Netbox Service that allows you to synchronize and update the interface data of your network devices in Netbox. This task ensures that the interface configurations in Netbox are accurate and up-to-date, reflecting the current state of your network infrastructure.

Keeping interface data accurate and up-to-date is crucial for effective network management. The Netbox Update Device Interfaces Task automates the process of updating interface information, such as interface names, statuses, mac addresses, and other relevant details.

How it works - Netbox worker on a call to update interfaces task fetches live data from network devices using nominated datasource, by default it is Nornir service parse task using NAPALM get_interfaces getter. Once data retrieved from network, Netbox worker updates records in Netbox database for device interfaces.

Netbox Update Device Interfaces

  1. Client submits and on-demand request to NorFab Netbox worker to update device interfaces

  2. Netbox worker sends job request to nominated datasource service to fetch live data from network devices

  3. Datasource service fetches data from the network

  4. Datasource returns devices interfaces data back to Netbox Service worker

  5. Netbox worker processes device interfaces data and updates records in Netbox for requested devices

Limitations¤

Datasource nornir uses NAPALM get_interfaces getter and as such only supports these device platforms:

  • Arista EOS
  • Cisco IOS
  • Cisco IOSXR
  • Cisco NXOS
  • Juniper JUNOS

Update Device Interfaces Sample Usage¤

NORFAB Netbox Update Device Interfaces Command Shell Reference¤

NorFab shell supports these command options for Netbox update_device_interfaces task:

nf# man tree netbox.update.device.interfaces
root
└── netbox:    Netbox service
    └── update:    Update Netbox data
        └── device:    Update device data
            └── interfaces:    Update device interfaces
                ├── timeout:    Job timeout
                ├── workers:    Filter workers to target, default 'any'
                ├── instance:    Netbox instance name to target
                ├── dry_run:    Return information that would be pushed to Netbox but do not push it
                ├── devices:    Devices to update
                └── datasource:    Service to use to retrieve device data, default 'nornir'
                    └── nornir:    Use Nornir service to retrieve data from devices
                        ├── add_details:    Add task details to results, default 'False'
                        ├── run_num_workers:    RetryRunner number of threads for tasks execution
                        ├── run_num_connectors:    RetryRunner number of threads for device connections
                        ├── run_connect_retry:    RetryRunner number of connection attempts
                        ├── run_task_retry:    RetryRunner number of attempts to run task
                        ├── run_reconnect_on_fail:    RetryRunner perform reconnect to host on task failure
                        ├── run_connect_check:    RetryRunner test TCP connection before opening actual connection
                        ├── run_connect_timeout:    RetryRunner timeout in seconds to wait for test TCP connection to establish
                        ├── run_creds_retry:    RetryRunner list of connection credentials and parameters to retry
                        ├── tf:    File group name to save task results to on worker file system
                        ├── tf_skip_failed:    Save results to file for failed tasks
                        ├── diff:    File group name to run the diff for
                        ├── diff_last:    File version number to diff, default is 1 (last)
                        └── progress:    Display progress events, default 'True'
nf#

Python API Reference¤

Function to update device interfaces in Netbox using information provided by NAPALM get_interfaces getter:

  • interface name
  • interface description
  • mtu
  • mac address
  • admin status
  • speed

Parameters:

Name Type Description Default
instance str

Netbox instance name

None
dry_run bool

return information that would be pushed to Netbox but do not push it

False
datasource str

service name to use to retrieve devices' data, default is nornir parse task

'nornir'
timeout int

seconds to wait before timeout data retrieval job

60
create bool

create missing interfaces

True
batch_size int

number of devices to process at a time

10
kwargs

any additional arguments to send to service for device data retrieval

{}

Returns:

Type Description
dict

dictionary keyed by device name with update details

Source code in norfab\workers\netbox_worker.py
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
def update_device_interfaces(
    self,
    instance: str = None,
    dry_run: bool = False,
    datasource: str = "nornir",
    timeout: int = 60,
    devices: list = None,
    create: bool = True,
    batch_size: int = 10,
    **kwargs,
) -> dict:
    """
    Function to update device interfaces in Netbox using information
    provided by NAPALM `get_interfaces` getter:

    - interface name
    - interface description
    - mtu
    - mac address
    - admin status
    - speed

    :param instance: Netbox instance name
    :param dry_run: return information that would be pushed to Netbox but do not push it
    :param datasource: service name to use to retrieve devices' data, default is nornir parse task
    :param timeout: seconds to wait before timeout data retrieval job
    :param create: create missing interfaces
    :param batch_size: number of devices to process at a time
    :param kwargs: any additional arguments to send to service for device data retrieval
    :returns: dictionary keyed by device name with update details
    """
    result = {}
    instance = instance or self.default_instance
    ret = Result(task=f"{self.name}:update_device_interfaces", result=result)
    nb = self._get_pynetbox(instance)

    if datasource == "nornir":
        for i in range(0, len(devices), batch_size):
            kwargs["FL"] = devices[i : i + batch_size]
            kwargs["getters"] = "get_interfaces"
            data = self.client.run_job(
                "nornir",
                "parse",
                kwargs=kwargs,
                workers="all",
                timeout=timeout,
            )
            for worker, results in data.items():
                if results["failed"]:
                    log.error(
                        f"{worker} get_interfaces failed, errors: {'; '.join(results['errors'])}"
                    )
                    continue
                for host, host_data in results["result"].items():
                    updated, created = {}, {}
                    result[host] = {
                        "update_device_interfaces_dry_run"
                        if dry_run
                        else "update_device_interfaces": updated,
                        "created_device_interfaces_dry_run"
                        if dry_run
                        else "created_device_interfaces": created,
                    }
                    interfaces = host_data["napalm_get"]["get_interfaces"]
                    nb_device = nb.dcim.devices.get(name=host)
                    if not nb_device:
                        raise Exception(f"'{host}' does not exist in Netbox")
                    nb_interfaces = nb.dcim.interfaces.filter(
                        device_id=nb_device.id
                    )
                    # update existing interfaces
                    for nb_interface in nb_interfaces:
                        if nb_interface.name not in interfaces:
                            continue
                        interface = interfaces.pop(nb_interface.name)
                        nb_interface.description = interface["description"]
                        nb_interface.mtu = interface["mtu"]
                        nb_interface.speed = interface["speed"] * 1000
                        nb_interface.mac_address = interface["mac_address"]
                        nb_interface.enabled = interface["is_enabled"]
                        if dry_run is not True:
                            nb_interface.save()
                        updated[nb_interface.name] = interface
                        self.event(
                            f"{host} updated interface {nb_interface.name}",
                            resource=instance,
                        )
                    # create new interfaces
                    if create is not True:
                        continue
                    for interface_name, interface in interfaces.items():
                        interface["type"] = "other"
                        nb_interface = nb.dcim.interfaces.create(
                            name=interface_name,
                            device={"name": nb_device.name},
                            type=interface["type"],
                        )
                        nb_interface.description = interface["description"]
                        nb_interface.mtu = interface["mtu"]
                        nb_interface.speed = interface["speed"] * 1000
                        nb_interface.mac_address = interface["mac_address"]
                        nb_interface.enabled = interface["is_enabled"]
                        if dry_run is not True:
                            nb_interface.save()
                        created[interface_name] = interface
                        self.event(
                            f"{host} created interface {nb_interface.name}",
                            resource=instance,
                        )
    else:
        raise UnsupportedServiceError(
            f"'{datasource}' datasource service not supported"
        )

    return ret