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.
Inputs¤
| Parameter | Required | Description |
|---|---|---|
parent |
Yes | Parent prefix as a network string, description string, or pynetbox filter dictionary |
description |
No | Description for the new prefix and deduplication key for existing prefixes |
prefixlen |
No | Length of the child prefix, default 30 |
vrf |
No | VRF name to associate with the prefix |
tags |
No | Tags to assign to the prefix |
tenant |
No | Tenant name to associate with the prefix |
comments |
No | Prefix comments |
role |
No | Prefix role |
site |
No | Site name to associate with the prefix |
status |
No | Prefix status |
instance |
No | NetBox instance name to target |
branch |
No | NetBox Branching plugin branch name to write to |
dry_run |
No | Preview the candidate prefix without writing |
Output¤
Returns the created or updated prefix data. In dry-run mode, returns the candidate prefix that would be allocated.
{
"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.- If
vrfis provided, the parent prefix must belong to the same VRF. - 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
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
result = client.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.0.0.0/24",
"description": "leaf-1 to spine-1",
},
)
# 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
├── 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 |
|---|---|---|---|
parent
|
Union[str, dict]
|
Parent prefix to allocate new prefix from, could be:
|
required |
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]
|
List of tags to assign 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
|
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
|
An object containing the outcome, including status, details of the prefix, and resources used. |
Source code in norfab\workers\netbox_worker\prefix_tasks.py
25 26 27 28 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 | |