Skip to content

Netbox Update Device Facts Task¤

Limitations¤

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

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

Update Device Facts Sample Usage¤

NORFAB Netbox Update Device Facts Command Shell Reference¤

NorFab shell supports these command options for Netbox update_device_facts task:

Python API Reference¤

Function to update device facts in Netbox using information provided by NAPALM get_facts getter:

  • serial number

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
kwargs

any additional arguments to send to service for device data retrieval

{}

Returns:

Type Description
dict

dictionary keyed by device name with updated details

Source code in norfab\workers\netbox_worker.py
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
def update_device_facts(
    self,
    instance: str = None,
    dry_run: bool = False,
    datasource: str = "nornir",
    timeout: int = 60,
    devices: list = None,
    **kwargs,
) -> dict:
    """
    Function to update device facts in Netbox using information
    provided by NAPALM get_facts getter:

    - serial number

    :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 kwargs: any additional arguments to send to service for device data retrieval
    :returns: dictionary keyed by device name with updated details
    """
    result = {}
    ret = Result(task=f"{self.name}:update_device_facts", result=result)
    nb = self._get_pynetbox(instance)
    kwargs["add_details"] = True

    if datasource == "nornir":
        if devices:
            kwargs["FL"] = devices
        data = self.client.run_job(
            "nornir",
            "parse",
            kwargs=kwargs,
            workers="all",
            timeout=timeout,
        )
        for worker, results in data.items():
            for host, host_data in results["result"].items():
                if host_data["napalm_get"]["failed"]:
                    log.error(
                        f"{host} - facts update failed: '{host_data['napalm_get']['exception']}'"
                    )
                    self.event(f"{host} - facts update failed")
                    continue
                nb_device = nb.dcim.devices.get(name=host)
                if not nb_device:
                    raise Exception(f"'{host}' does not exist in Netbox")
                facts = host_data["napalm_get"]["result"]["get_facts"]
                # update serial number
                nb_device.serial = facts["serial_number"]
                if not dry_run:
                    nb_device.save()
                result[host] = {
                    "update_device_facts_dry_run"
                    if dry_run
                    else "update_device_facts": {
                        "serial": facts["serial_number"],
                    }
                }
                self.event(f"{host} - facts updated")
    else:
        raise UnsupportedServiceError(f"'{datasource}' service not supported")

    return ret