Netbox Create Prefix Task¤
task api name:
create_prefix
Allocates the next available child prefix from a parent prefix, or updates an existing matching prefix. By default it creates a /30, which is useful for point-to-point subnets.
The task can also be called from Nornir templates through the netbox.create_prefix Jinja2 filter.
Output¤
Returns the created or updated prefix data. In dry-run mode, returns the candidate prefix that would be allocated. The preview uses the first available range that can fit the requested prefix length.
{
"prefix": "10.0.0.0/30",
"description": "leaf-1 to spine-1",
"status": "active",
"...": "...",
}
Notes / Gotchas¤
Warning
create_prefix uses the description argument to find existing prefixes. Use the same description for repeated calls that should refer to the same prefix.
parentcan be a prefix string such as10.0.0.0/24, a parent prefix description, or a dictionary of pynetbox prefix filters.- To associate an existing VLAN, supply
vlan(VID, 1-4094) andvlan_group(group name) together. A VID and site alone are unsupported;sitescopes the prefix and does not identify the VLAN. Missing or ambiguous VLAN/group lookups fail before allocation. Omission preserves an existing association. - VLAN changes appear in
diff.vlanas old/newVID (ID)values, including dry-run previews. Changing VLAN updates the prefix selected by the existing description/scope filters rather than allocating a replacement. - If
vrfis provided, the parent prefix must belong to the same VRF. - When updating an existing prefix, new tags and new items in list-valued custom
fields are added without removing current values. Supplying
[]leaves an existing custom-field list unchanged. For custom fields,nullclears the value, and a new scalar value replaces the old one. - Object and multiobject custom fields accept related object names or IDs. The task resolves names using each field's NetBox definition; missing or ambiguous names fail.
- Branch writes require the NetBox Branching Plugin.
Examples¤
Allocate the next /30 from a parent prefix:
nf#netbox create prefix parent 10.0.0.0/24 description "leaf-1 to spine-1"
Allocate a /31 in a VRF:
nf#netbox create prefix parent 10.0.0.0/24 description "leaf-1 to spine-1" prefixlen 31 vrf PROD
Associate VLAN 100 from an existing group:
nf#netbox create prefix parent 10.0.0.0/24 description "users" vlan 100 vlan_group "LAB VLANS"
Preview the allocation:
nf#netbox create prefix parent 10.0.0.0/24 description "leaf-1 to spine-1" dry-run
Write into a NetBox branch:
nf#netbox create prefix parent 10.0.0.0/24 description "leaf-1 to spine-1" branch my-branch
from norfab.core.nfapi import NorFab
nf = NorFab(inventory="./inventory.yaml")
nf.start()
client = nf.make_client()
# allocate next available /30; VLAN 100 in LAB VLANS must already exist
result = client.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.0.0.0/24",
"description": "leaf-1 to spine-1",
"vlan": 100,
"vlan_group": "LAB VLANS",
},
)
# allocate from a filtered parent prefix
result = client.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": {"prefix": "10.0.0.0/24", "site": "lab"},
"description": "leaf-2 to spine-1",
"prefixlen": 31,
"vrf": "PROD",
"tags": ["automation-managed"],
},
)
nf.destroy()
NORFAB Netbox Create Prefix Command Shell Reference¤
NorFab shell supports these command options for Netbox create_prefix task:
nf# man tree netbox.create.prefix
root
└── netbox: Netbox service
└── create: Create objects in Netbox
└── prefix: Allocate next available prefix from parent prefix
├── instance: Netbox instance name to target
├── dry-run: Do not commit to database
├── parent: Parent prefix to allocate new prefix from
├── description: Description for new prefix
├── prefixlen: The prefix length of the new prefix, default '30'
├── vrf: Name of the VRF to associate with the prefix
├── tags: List of tags to assign to the prefix
├── tenant: Name of the tenant to associate with the prefix
├── comments: Comments for the prefix
├── role: Role to assign to the prefix
├── site: Name of the site to associate with the prefix
├── vlan: Existing VLAN VID; requires vlan_group
├── vlan_group: Existing VLAN group name; requires vlan
├── status: Status of the prefix
├── branch: Branching plugin branch name to use
├── timeout: Job timeout
├── workers: Filter worker to target, default 'any'
├── verbose-result: Control output details, default 'False'
└── progress: Display progress events, default 'True'
nf#
Python API Reference¤
Creates a new IP prefix in NetBox or updates an existing one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
NorFab job supplying progress events and execution context. |
required |
parent
|
Union[str, dict]
|
Parent prefix to allocate new prefix from, could be:
|
required |
custom_fields
|
dict
|
Custom-field names and values to PATCH on the allocated prefix. New list items are added to existing custom-field lists; scalar values and null replace old values. Object and multiobject fields accept related object names or IDs. Definitions must already exist in NetBox. The result diff lists changed field names only, not their values. |
None
|
description
|
str
|
Description for the new prefix, prefix description used for deduplication to source existing prefixes. |
None
|
prefixlen
|
int
|
The prefix length of the new prefix to create, by default allocates next available /30 point-to-point prefix. |
30
|
vrf
|
str
|
Name of the VRF to associate with the prefix. |
None
|
tags
|
Union[None, list]
|
Tag names to add to the prefix. |
None
|
tenant
|
str
|
Name of the tenant to associate with the prefix. |
None
|
comments
|
str
|
Comments for the prefix. |
None
|
role
|
str
|
Role to assign to the prefix. |
None
|
site
|
str
|
Name of the site to associate with the prefix. |
None
|
vlan
|
int
|
Existing VLAN VID (1-4094), identified together with vlan_group. Omission preserves the current association. Site cannot substitute for the VLAN group. |
None
|
vlan_group
|
str
|
Existing VLAN group name. Must be supplied together with vlan; neither the group nor VLAN is created. |
None
|
status
|
str
|
Status of the prefix. |
None
|
instance
|
Union[None, str]
|
NetBox instance identifier. |
None
|
dry_run
|
bool
|
If True, simulates the creation without making changes. |
False
|
branch
|
str
|
Branch name to use, need to have branching plugin installed, automatically creates branch if it does not exist in Netbox. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Result |
Result
|
Prefix details, status, resources, and changed fields. VLAN changes use old/new "VID (ID)" values in diff["vlan"]. Dry runs report the planned association without writing it. |
Raises:
| Type | Description |
|---|---|
NetboxAllocationError
|
The parent or requested VLAN/group cannot be resolved uniquely, allocation fails, or an existing prefix has a different length. VLAN lookup failures occur before allocation. |
Source code in norfab\workers\netbox_worker\prefix_tasks.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 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 | |