Netbox Create IP Task¤
task api name:
create_ip
Allocates the next available IP address from a parent prefix, or reuses an existing matching IP address. The task can assign the IP to a device interface, update metadata, set the address as primary, and optionally create an address for the connected peer interface.
Set ip_index to select a usable address by its one-based position in the subnet. For example, index 1 selects .1 in an IPv4 /30, while indexes 1 and 2 select both addresses of a /31. An index beyond the usable addresses fails. Existing group or interface assignments are reused before allocating a child subnet. Dry runs with mask_len still preview an address from the parent prefix.
An existing assignment to the requested interface is reused first. If none is found, ip_index selects a specific usable address; without it, the task allocates the next available address. The requested role, including anycast or vip, is set on the allocated IP. If an indexed address already exists, the task can create another record for anycast or vip after setting the existing record's role. An existing indexed address with another requested role causes an error.
The task can also be called from Nornir templates through the netbox.create_ip Jinja2 filter.
Interface reuse searches by device, interface, and parent prefix. A supplied description prefers an exact match; if none matches, the sole candidate is reused and its description is updated. Multiple remaining candidates cause an ambiguity error. This applies to ordinary, anycast, and VIP addresses, before indexed allocation. Description-only and VRRP-group lookups retain their description matching behavior.
VRRP virtual IP allocation¤
Supply vrrp_group with an existing, uniquely named VRRPv2 or VRRPv3 group.
The task assigns the address to that group and defaults its role to vrrp.
Repeated calls reuse the group's address within the selected prefix, optionally
matching description. Missing, ambiguous, or non-VRRP groups fail before allocation.
create_ip:
prefix:
site: LAB
role: MGMT
vrrp_group: LAB-mgmt
Group allocation cannot accompany device, interface, or is_primary: true.
ip_index selects the initial group address; later calls reuse the group's IP.
It disables peer allocation. Dry runs resolve the group without writing; the
existing dry-run limitation for mask_len still applies. Results include
vrrp_group when supplied. Omitting it preserves existing task behavior.
Output¤
Returns the allocated or updated IP address data. When create_peer_ip=True, peer allocation details may be included in the result.
{
"address": "10.0.0.1/31",
"device": "ceos-leaf-1",
"interface": "Ethernet1",
"description": "leaf-1 to spine-1",
"...": "...",
}
Notes / Gotchas¤
prefixcan be a network string, a prefix description, or a dictionary of pynetbox prefix filters.- When
mask_lendiffers from the parent prefix length, the task creates or reuses a child prefix throughcreate_prefix. - If no child subnet can be selected, the task returns an error that includes any failed availability checks.
- In dry-run mode,
mask_lenis ignored and the candidate IP is allocated directly from the parent prefix. - IPv4 /32 and IPv6 /128 allocations automatically disable peer creation and peer-subnet reuse, including when the supplied parent is itself a host prefix.
- If peer allocation fails, the task returns the peer error in
errors; the local IP may already be allocated. ip_indexis one-based over usable IPs;/31and/127include both endpoints.- Object and multiobject custom fields accept related object names or IDs. The task resolves names using each field's NetBox definition, adds new list values on repeated calls, and replaces scalar or
nullvalues. Missing or ambiguous names fail. - Branch writes require the NetBox Branching Plugin.
Examples¤
Allocate an IP for an interface:
nf#netbox create ip prefix 10.0.0.0/24 device ceos-leaf-1 interface Ethernet1 description "leaf-1 uplink"
Allocate a /31 link subnet and create the peer IP:
nf#netbox create ip prefix 10.0.0.0/24 device ceos-leaf-1 interface Ethernet1 mask-len 31 create-peer-ip
Set DNS name and mark the address as primary:
nf#netbox create ip prefix loopbacks device ceos-leaf-1 interface Loopback0 dns-name ceos-leaf-1.example.net is-primary
Preview allocation without writing:
nf#netbox create ip prefix 10.0.0.0/24 device ceos-leaf-1 interface Ethernet1 dry-run
from norfab.core.nfapi import NorFab
nf = NorFab(inventory="./inventory.yaml")
nf.start()
client = nf.make_client()
# allocate an IP for an interface
result = client.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "ceos-leaf-1",
"interface": "Ethernet1",
"description": "leaf-1 uplink",
},
)
# allocate a /31 link subnet and peer IP
result = client.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": {"prefix": "10.0.0.0/24", "site": "lab"},
"device": "ceos-leaf-1",
"interface": "Ethernet1",
"mask_len": 31,
"create_peer_ip": True,
},
)
nf.destroy()
NORFAB Netbox Create IP Command Shell Reference¤
NorFab shell supports these command options for Netbox create_ip task:
nf# man tree netbox.create.ip
root
└── netbox: Netbox service
└── create: Create objects in Netbox
└── ip: Allocate next available IP address from prefix
├── timeout: Job timeout
├── workers: Filter worker to target, default 'any'
├── verbose-result: Control output details, default 'False'
├── progress: Display progress events, default 'True'
├── instance: Netbox instance name to target
├── dry-run: Do not commit to database
├── prefix: Prefix to allocate IP address from, can also provide prefix name or filters
├── device: Device name to associate IP address with
├── interface: Device interface name to associate IP address with
├── 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
├── status: IP address status
├── is-primary: Set the IP address as the primary IP for the device
├── mask-len: Mask length to use for IP address
├── create-peer-ip: Create link peer IP address as well
└── branch: Branching plugin branch name to use
nf#
Python API Reference¤
Allocate the next available or indexed usable IP address from a subnet.
.. warning::
**Dry-run limitation**: when ``dry_run=True``, ``mask_len`` is ignored and
the candidate IP address is allocated directly from the parent prefix using
the parent prefix mask length — no child subnet is created.
This task finds or creates an IP address in NetBox, updates its metadata,
optionally links it to a device/interface, and supports a dry run mode for
previewing changes. Existing group or interface assignments are reused
before ip_index selects an address for a new allocation. An indexed
address already in NetBox can be duplicated for anycast or VIP roles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
The prefix from which to allocate the IP address, could be:
|
required |
custom_fields
|
dict
|
Custom-field names and values to PATCH on the allocated IP. Object and multiobject fields accept related object names or IDs. List values add to existing values; scalar values and null replace them. Definitions must exist in NetBox. |
None
|
description
|
str
|
A description for the allocated IP address. For device/interface reuse, prefers an exact description match; otherwise reuses the sole address within the selected prefix and updates its description. Multiple remaining candidates raise NetboxAllocationError instead of allocating another address. |
None
|
device
|
str
|
The device associated with the IP address. |
None
|
interface
|
str
|
The interface associated with the IP address. |
None
|
is_primary
|
bool
|
If True, set the allocated IPv4 or IPv6 address as the device's primary IP, including when already assigned. |
None
|
vrrp_group
|
str
|
Existing VRRP group name to assign the IP to. Must uniquely identify a VRRPv2 or VRRPv3 group. Reuses an IP assigned to that group within the selected prefix, optionally matching description. Cannot accompany device, interface, or is_primary=True. Disables peer allocation and defaults role to vrrp. Missing or ambiguous groups raise NetboxAllocationError before allocation. Returned when supplied. |
None
|
vrf
|
str
|
The VRF (Virtual Routing and Forwarding) instance. |
None
|
tags
|
list
|
A list of tags to associate with the IP address. |
None
|
dns_name
|
str
|
The DNS name for the IP address. |
None
|
tenant
|
str
|
The tenant associated with the IP address. |
None
|
comments
|
str
|
Additional comments for the IP address. |
None
|
status
|
str
|
Status for the IP address, such as active, reserved, or deprecated. Updates an existing IP when different. |
None
|
instance
|
str
|
The NetBox instance to use. |
None
|
dry_run
|
bool
|
If True, do not actually allocate the IP address. |
False
|
branch
|
str
|
Branch name to use, need to have branching plugin installed, automatically creates branch if it does not exist in Netbox. |
None
|
mask_len
|
int
|
mask length to use for IP address on creation or to
update existing IP address. On new IP address creation will create child
subnet of |
None
|
ip_index
|
int
|
One-based position among usable IPs in the
selected subnet. On a /31 or /127, both addresses are usable.
Out-of-range positions raise |
None
|
create_peer_ip
|
bool
|
If True creates IP address for link peer - remote device interface connected to requested device and interface. Disabled for IPv4 /32 and IPv6 /128 allocations, which have no room for a peer. Host allocations also skip peer-subnet reuse. Peer allocation errors are returned while the local IP remains allocated. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
Result |
Result
|
Allocation details. If no child subnet can be selected, the result is marked failed and includes any subnet probe errors. |
Tasks execution follow these steps:
-
Tries to find an existing IP in NetBox within the selected prefix. For device/interface assignments, prefers a matching description or reuses the sole candidate, including anycast and VIP addresses. Ambiguous candidates fail; no candidates proceeds to allocation.
-
If prefix is a string, determines if it's an IP network or a description. Builds a filter dictionary for NetBox queries, optionally including VRF.
-
Queries NetBox for the prefix using the constructed filter.
-
If dry_run is True, fetches the next available IP but doesn't create it.
-
If not a dry run, creates the next available IP in the prefix.
-
Updates IP attributes (description, VRF, tenant, DNS name, comments, role, tags) if provided and different from current values. Handles interface assignment and can set the IP as primary for the device.
-
If changes were made and not a dry run, saves the IP and device updates to NetBox.
Source code in norfab\workers\netbox_worker\ip_tasks.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 | |