NetBox Get Devices Task¤
task api name:
get_devices
Retrieves device records from NetBox using the REST API. Results are keyed by device name and include the full device payload returned by pynetbox, with site data expanded for each device.
Inputs¤
| Parameter | Required | Description |
|---|---|---|
filters |
No | List of NetBox device filter dictionaries |
devices |
No | Device names to retrieve |
instance |
No | NetBox instance name to target |
dry_run |
No | Return the merged filters without querying NetBox |
cache |
No | Cache usage mode: True, False, refresh, or force |
Output¤
Normal mode returns a dictionary keyed by device name:
{
"ceos-leaf-1": {
"name": "ceos-leaf-1",
"status": {"value": "active", "label": "Active"},
"site": {"name": "lab", "...": "..."},
"platform": {"name": "eos"},
"primary_ip4": {"address": "192.0.2.11/32"},
"...": "...",
},
}
Notes / Gotchas¤
- When both
devicesandfiltersare provided, device names are merged into the filter list as a NetBoxnamefilter. cache=Trueuses cached device data whenlast_updatedmatches NetBox.cache="force"uses cached data without the freshness check.cache=Falseskips cache reads and writes.cache="refresh"fetches fresh data and overwrites cache.
Examples¤
Get specific devices:
nf#netbox get devices devices ceos-leaf-1 ceos-leaf-2
Filter devices using a NetBox filter dictionary:
nf#netbox get devices filters '[{"site": "lab", "status": "active"}]'
Preview the merged filters without querying NetBox:
nf#netbox get devices devices ceos-leaf-1 dry-run
Refresh cached device data:
nf#netbox get devices devices ceos-leaf-1 cache refresh
Context manager:
from norfab.core.nfapi import NorFab
with NorFab(inventory="./inventory.yaml") as nf:
client = nf.make_client()
result = client.run_job(
"netbox",
"get_devices",
workers="any",
kwargs={
"devices": ["ceos-leaf-1", "ceos-leaf-2"],
},
)
Direct lifecycle:
from norfab.core.nfapi import NorFab
nf = NorFab(inventory="./inventory.yaml")
try:
nf.start()
client = nf.make_client()
result = client.run_job(
"netbox",
"get_devices",
workers="any",
kwargs={
"filters": [{"site": "lab", "status": "active"}],
},
)
preview = client.run_job(
"netbox",
"get_devices",
workers="any",
kwargs={
"devices": ["ceos-leaf-1"],
"dry_run": True,
},
)
finally:
nf.destroy()
NORFAB Netbox Get Devices Command Shell Reference¤
NorFab shell supports these command options for Netbox get_devices task:
nf# man tree netbox.get.devices
root
└── netbox: Netbox service
└── get: Query data from Netbox
└── devices: Query Netbox devices data
├── instance: Netbox instance name to target
├── workers: Filter worker to target, default 'any'
├── timeout: Job timeout
├── filters: List of device filters dictionaries as a JSON string, examples: [{"q": "ceos1"}]
├── devices: Device names to query data for
├── dry-run: Only return query content, do not run it
└── cache: How to use cache, default 'True'
nf#
Python API Reference¤
Retrieve device data from Netbox REST API using Pynetbox.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
NorFab Job object |
required |
filters
|
Union[None, list]
|
list of filter dicts applied to |
None
|
instance
|
Union[None, str]
|
Netbox instance name, uses default if omitted |
None
|
dry_run
|
bool
|
if True returns filter params without making REST calls |
False
|
devices
|
Union[None, list]
|
list of device names to fetch, merged into filters as |
None
|
cache
|
Union[None, bool, str]
|
|
None
|
Returns:
| Type | Description |
|---|---|
Result
|
dict keyed by device name with fields: last_updated, custom_field_data, tags, device_type, |
Result
|
role, config_context, tenant, platform, serial, asset_tag, site, location, rack, status, |
Result
|
primary_ip4, primary_ip6, airflow, position, id |
Source code in norfab\workers\netbox_worker\devices_tasks.py
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 | |