Skip to content

Netbox Update Device IP Task¤

task api name: update_device_ip

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

How it works - Netbox worker on a call to update IP addresses task fetches live data from network devices using nominated datasource, by default it is Nornir service parse task using NAPALM get_interfaces_ip 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 IP addresses

  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 IP addresses data back to Netbox Service worker

  5. Netbox worker processes device data and updates or creates IP address records in Netbox for requested devices

Limitations¤

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

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

Update Device IP Sample Usage¤

NORFAB Netbox Update Device IP Command Shell Reference¤

NorFab shell supports these command options for Netbox update_device_ip task:

nf#man tree netbox.update.device.ip-addresses
root
└── netbox:    Netbox service
    └── update:    Update Netbox data
        └── device:    Update device data
            └── ip-addresses:    Update device interface IP addresses
                ├── 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:    List of Netbox devices to update
                │   └── 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'
                └── batch-size:    Number of devices to process at a time, default '10'
nf#

Python API Reference¤

Update the IP addresses of devices in Netbox.

Parameters:

Name Type Description Default
instance str

The instance name to use.

None
dry_run bool

If True, no changes will be made.

False
datasource str

The data source to use. Supported datasources:

  • nornir - uses Nornir Service parse task to retrieve devices' data using NAPALM get_interfaces_ip getter
'nornir'
timeout int

The timeout for the operation.

60
devices list

The list of devices to update.

None
create bool

If True, new IP addresses will be created if they do not exist.

True
batch_size int

The number of devices to process in each batch.

10
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
dict dict

A dictionary containing the results of the update operation.

Raises:

Type Description
Exception

If a device does not exist in Netbox.

UnsupportedServiceError

If the specified datasource is not supported.

Source code in norfab\workers\netbox_worker.py
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
def update_device_ip(
    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:
    """
    Update the IP addresses of devices in Netbox.

    Args:
        instance (str, optional): The instance name to use.
        dry_run (bool, optional): If True, no changes will be made.
        datasource (str, optional): The data source to use. Supported datasources:

            - **nornir** - uses Nornir Service parse task to retrieve devices' data
                using NAPALM get_interfaces_ip getter

        timeout (int, optional): The timeout for the operation.
        devices (list, optional): The list of devices to update.
        create (bool, optional): If True, new IP addresses will be created if they do not exist.
        batch_size (int, optional): The number of devices to process in each batch.
        **kwargs: Additional keyword arguments.

    Returns:
        dict: A dictionary containing the results of the update operation.

    Raises:
        Exception: If a device does not exist in Netbox.
        UnsupportedServiceError: If the specified datasource is not supported.
    """
    result = {}
    instance = instance or self.default_instance
    ret = Result(task=f"{self.name}:update_device_ip", 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_ip"
            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_ip failed, errors: {'; '.join(results['errors'])}"
                    )
                    continue
                for host, host_data in results["result"].items():
                    updated, created = {}, {}
                    result[host] = {
                        "updated_ip_dry_run" if dry_run else "updated_ip": updated,
                        "created_ip_dry_run" if dry_run else "created_ip": created,
                    }
                    interfaces = host_data["napalm_get"]["get_interfaces_ip"]
                    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 interface IP addresses
                    for nb_interface in nb_interfaces:
                        if nb_interface.name not in interfaces:
                            continue
                        interface = interfaces.pop(nb_interface.name)
                        # merge v6 into v4 addresses to save code repetition
                        interface["ipv4"].update(interface.pop("ipv6", {}))
                        # update/create IP addresses
                        for ip, ip_data in interface["ipv4"].items():
                            prefix_length = ip_data["prefix_length"]
                            # get IP address info from Netbox
                            nb_ip = nb.ipam.ip_addresses.filter(
                                address=f"{ip}/{prefix_length}"
                            )
                            if len(nb_ip) > 1:
                                log.warning(
                                    f"{host} got multiple {ip}/{prefix_length} IP addresses from Netbox, "
                                    f"NorFab Netbox Service only supports handling of non-duplicate IPs."
                                )
                                continue
                            # decide what to do
                            if not nb_ip and create is False:
                                continue
                            elif not nb_ip and create is True:
                                if dry_run is not True:
                                    nb_ip = nb.ipam.ip_addresses.create(
                                        address=f"{ip}/{prefix_length}"
                                    )
                                    nb_ip.assigned_object_type = "dcim.interface"
                                    nb_ip.assigned_object_id = nb_interface.id
                                    nb_ip.status = "active"
                                    nb_ip.save()
                                created[f"{ip}/{prefix_length}"] = nb_interface.name
                                self.event(
                                    f"{host} created IP address {ip}/{prefix_length} for {nb_interface.name} interface",
                                    resource=instance,
                                )
                            elif nb_ip:
                                nb_ip = list(nb_ip)[0]
                                nb_ip.assigned_object_type = "dcim.interface"
                                nb_ip.assigned_object_id = nb_interface.id
                                nb_ip.status = "active"
                                if dry_run is not True:
                                    nb_ip.save()
                                updated[nb_ip.address] = nb_interface.name
                                self.event(
                                    f"{host} updated IP address {ip}/{prefix_length} for {nb_interface.name} interface",
                                    resource=instance,
                                )

    else:
        raise UnsupportedServiceError(
            f"'{datasource}' datasource service not supported"
        )

    return ret