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\ip_tasks.py
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
@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
    log.info(
        f"{self.name} - Create IP bulk: Assigning IPs for {len(devices or [])} device(s) from '{instance}' Netbox"
    )
    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