Skip to content

Netbox Create IP Bulk Task¤

task api name: create_ip_bulk

Task to allocate next available IP from prefix or get existing IP address for multiple devices and interfaces.

Branching Support¤

Create IP task is branch aware and can create IP addresses within the branch. Netbox Branching Plugin need to be installed on Netbox instance.

NORFAB Netbox Create IP Command Shell Reference¤

NorFab shell supports these command options for Netbox create_ip task:

nf# man tree netbox.create.ip_bulk
root
└── netbox:    Netbox service
    └── create:    Create objects in Netbox
        └── ip-bulk:    Allocate next available IP address from prefix for multiple devices and interfaces
            ├── timeout:    Job timeout
            ├── workers:    Filter worker to target, default 'any'
            ├── verbose-result:    Control output details, default 'False'
            ├── progress:    Display progress events, default 'True'
            ├── nowait:    Do not wait for job to complete, default 'False'
            ├── instance:    Netbox instance name to target
            ├── dry-run:    Do not commit to database
            ├── branch:    Branching plugin branch name to use
            ├── *prefix:    Prefix to allocate IP address from, can also provide prefix name or filters
            ├── *devices:    List of device names to create IP address for
            ├── interface_regex:    Regular expression of device interface names to create IP address for
            ├── interface_list:    List of interface names to create IP address for
            ├── description:    IP address description
            ├── vrf:    VRF to associate with IP address
            ├── tags:    Tags to add to IP address
            ├── dns_name:    IP address DNS name
            ├── tenant:    Tenant name to associate with IP address
            ├── comments:    IP address comments field
            ├── role:    IP address functional role
            ├── mask-len:    Mask length to use for IP address
            ├── create-peer-ip:    Create link peer IP address as well
            └── status:    IP address status
nf#

Python API Reference¤

Bulk assigns IP addresses to interfaces of specified devices.

Parameters:

Name Type Description Default
job Job

The job instance used for logging and tracking the task.

required
prefix Union[str, dict]

The IP prefix or a dictionary containing prefix details.

required
devices list[str]

A list of device names to process. Defaults to None.

None
interface_list list[str]

A list of specific interfaces to target. Defaults to None.

None
interface_regex str

A regex pattern to match interfaces. Defaults to None.

None
instance Union[None, str]

The instance name to use. Defaults to None.

None
kwargs dict

Additional arguments to pass to the create_ip method calls.

{}

Returns:

Name Type Description
Result Result

A Result object containing the task details, results, and resources.

Notes
  • If both interface_list and interface_regex are provided, interface_list takes precedence.
  • The prefix parameter can be a string representing the prefix or a dictionary with additional details.
Source code in norfab\workers\netbox_worker\netbox_worker.py
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
@Task(
    fastapi={"methods": ["POST"], "schema": NetboxFastApiArgs.model_json_schema()}
)
def create_ip_bulk(
    self,
    job: Job,
    prefix: Union[str, dict],
    devices: list[str] = None,
    interface_list: list[str] = None,
    interface_regex: str = None,
    instance: Union[None, str] = None,
    **kwargs,
) -> Result:
    """
    Bulk assigns IP addresses to interfaces of specified devices.

    Args:
        job (Job): The job instance used for logging and tracking the task.
        prefix (Union[str, dict]): The IP prefix or a dictionary containing prefix details.
        devices (list[str], optional): A list of device names to process. Defaults to None.
        interface_list (list[str], optional): A list of specific interfaces to target. Defaults to None.
        interface_regex (str, optional): A regex pattern to match interfaces. Defaults to None.
        instance (Union[None, str], optional): The instance name to use. Defaults to None.
        kwargs (dict, optional): Additional arguments to pass to the `create_ip` method calls.

    Returns:
        Result: A Result object containing the task details, results, and resources.

    Notes:
        - If both `interface_list` and `interface_regex` are provided, `interface_list` takes precedence.
        - The `prefix` parameter can be a string representing the prefix or a dictionary with additional details.
    """
    instance = instance or self.default_instance
    ret = Result(
        task=f"{self.name}:create_ip_bulk", result={}, resources=[instance]
    )

    # get list of all interfaces
    interfaces = self.get_interfaces(
        job=job,
        devices=devices,
        interface_list=interface_list,
        interface_regex=interface_regex,
        instance=instance,
    )

    # iterate over interfaces and assign IP addresses
    for device, device_interfaces in interfaces.result.items():
        ret.result[device] = {}
        for interface in sorted(device_interfaces.keys()):
            create_ip = self.create_ip(
                job=job,
                device=device,
                interface=interface,
                instance=instance,
                prefix=prefix,
                **kwargs,
            )
            ret.result[device][interface] = create_ip.result

    return ret