Netbox Sync Device Inventory Task¤
task api name:
sync_device_inventory
The sync_device_inventory task reconciles live hardware inventory collected
from the Nornir service with NetBox device and module data.
Live inventory is collected with Nornir parse_ttp using get="inventory".
The chassis inventory record updates dcim.device.serial. Non-chassis
inventory records, including optics and transceivers, are managed as NetBox
dcim.modules installed in device-level dcim.module_bays.
Parsed Inventory Records¤
The Nornir inventory parser and optional inventory transformer must return one list of records per device. Every record has exactly these fields:
- description: "ASR9K Route Switch Processor with 440G/slot Fabric and 6GB"
slot: "module 0/RSP0/CPU0"
module: "A9K-RSP440-TR"
serial: "M9YXCZV9QF"
Each field value must be a string or null.
Device Serial Number¤
A record whose trimmed slot value is chassis, case-insensitive, is the device chassis record:
- description: "Cisco ASR 9006 Router"
slot: "chassis"
module: "ASR-9006"
serial: "JCY98XR393D"
For this record, the task:
- reads
serialas the desired NetBoxdcim.device.serial; - creates a synthetic
chassisentry in the inventory diff; - updates the NetBox device serial when the live and NetBox values differ;
- does not create a module bay, module type, or installed module for it.
The chassis module and description values do not participate in the device
serial comparison. An empty or BUILTIN chassis serial is ignored. Multiple
chassis records with different non-empty serial numbers produce a per-device
error and that device is skipped.
Safety Controls¤
Module deletions are disabled by default. Set process_deletions=True to
delete NetBox modules that are absent from live network inventory.
Missing module bays and module types are reported by default. Set
create_module_bays=True or create_module_types=True when the task should
extend NetBox modeling from live inventory.
Live records with empty module identity, empty serial numbers, or BUILTIN
serial numbers are skipped and reported in res["errors"]. Skipped slots
suppress deletion so incomplete live data does not remove existing NetBox
modules.
Filtering Modules¤
The task can select or exclude modules using lists of case-sensitive glob patterns:
filter_by_module: include normalized module type names matching any pattern.filter_by_slot: include normalized module bay names matching any pattern.ignore_modules: exclude normalized module type names matching any pattern.ignore_slots: exclude normalized module bay names matching any pattern.
Filters run after the optional Python transformer, inventory name mapping, and
built-in live-data normalization. A pattern therefore matches the final
module_type and slot values used for NetBox comparison, not necessarily the
raw parser values.
Patterns within one argument use OR logic. When both include filters are provided, a module must match both dimensions. Ignore patterns take precedence: a match in either ignore list excludes the module.
The chassis record is never filtered, so device serial synchronization remains
independent from module selection. The same comparison scope is applied to
existing NetBox modules. A live slot excluded by a filter is also excluded from
the NetBox side, preventing it from being treated as stale when
process_deletions=True.
Example:
filter_by_module:
- "A9K-*"
- "SFP-10G-*"
filter_by_slot:
- "module 0/*"
ignore_modules:
- "*-BUILTIN"
ignore_slots:
- "module 0/PS*"
Inventory Name Mapping¤
Use inventory_map when live module or slot names differ from existing NetBox
module type models and module bay names. Supply either an inline mapping or an
nf:// reference to a YAML file available through the NorFab File Service.
The mapping is applied after an optional Python transformer and before
inventory normalization and diff calculation.
module_types:
Cisco:
"ASR 9000 RSP440":
- glob: "A9K-RSP440-*"
"10GBASE-LR SFP+":
- regex: "^SFP-10G-LR(=)?$"
"ASR 9000 Fan Tray":
- eval: "value == 'ASR-9006-FAN-V2'"
module_bays:
Cisco:
"ASR-9006":
"0/RSP0":
- glob: "module 0/RSP0/*"
"0/1":
- regex: "^module 0/1(/CPU0)?$"
"TenGigE0/2/CPU0/0":
- eval: "value == 'module mau TenGigE0/2/CPU0/0'"
File reference example:
inventory_map: "nf://netbox/inventory_maps/iosxr.yaml"
The downloaded YAML is parsed with yaml.safe_load and validated with the
same Pydantic model used for inline mappings. Invalid YAML, invalid mapping
conditions, and missing files fail the task before any NetBox writes.
Mapping Scope¤
module_typesis selected using the exact, case-sensitive NetBox device manufacturername.- A module type target key is the desired NetBox module type
model. - Module type conditions test the live record's
modulevalue. module_baysis selected using the exact NetBox manufacturer name followed by the exact NetBox device typemodel.- A module bay target key is the desired NetBox module bay
name. - Module bay conditions test the live record's
slotvalue. - Chassis records are not mapped.
Each condition contains exactly one matcher:
globuses case-sensitive glob matching.regexperforms a full regular-expression match.evalevaluates trusted Python configuration with the live string available asvalue.
Conditions under one target use OR logic. If one target matches, its key
replaces the live value. If nothing matches, the original value is retained.
If multiple targets match, the record is skipped and the ambiguity is added to
res["errors"].
Warning
eval is trusted executable configuration, not a security sandbox. Only
accept inventory mappings from trusted sources.
Python Inventory Transformer¤
Use inventory_transform for normalization that cannot be expressed with
pattern mappings. Its value is an nf:// reference to a Python file available
through the NorFab File Service:
inventory_transform: "nf://netbox/inventory_transformers/iosxr.py"
The file must export a function named transform with this contract:
def transform(
device_name: str,
parsed_data: list,
worker: object,
device_platform: str | None = None,
device_manufacturer: str | None = None,
device_type: str | None = None,
) -> list[dict]:
"""Normalize parsed inventory records for one device."""
transformed_data = []
for record in parsed_data:
slot = record["slot"]
if slot.startswith("module mau "):
record["slot"] = slot.removeprefix("module mau ")
if (
device_manufacturer == "Cisco"
and device_type == "ASR-9006"
and record["module"] == "A9K-RSP440-TR"
):
record["module"] = "ASR 9000 RSP440"
transformed_data.append(record)
return transformed_data
The arguments are:
device_name: current NetBox and Nornir device name.parsed_data: parsed records for that device, list of dictionaries withslot,serial,description,modulekeys.worker: active NetBox worker object, including its configuration and all worker helper methods.device_platform: NetBox device platformname, orNonewhen no platform is assigned.device_manufacturer: NetBox device type manufacturername.device_type: NetBox device typemodel.
The return value must be a list of dictionaries. Every dictionary must contain
exactly description, slot, module, and serial, with each value set to a
string or None. Returning an empty list means there is no usable inventory
for that device. Invalid output or an exception skips that device and records
the validation or execution error in result's errors.
The transformer is loaded once per task and called once per device. It runs
before inventory_map, so pattern conditions test the transformed module
and slot values. The device metadata arguments are read from NetBox before
the transformer runs, so they describe the current NetBox device, not the live
inventory record.
Warning
Transformer files are executed as trusted Python code. Store them only in controlled File Service locations.
Branching Support¤
This task is branch aware and can push updates to a NetBox branch when the
NetBox Branching plugin is installed. Use the branch argument to target a
branch.
Result Structure¤
Dry-run mode (dry_run=True) returns the raw diff without writing to
NetBox:
{
"<device>": {
"create": ["module 0/RSP0/CPU0", "module mau 0/1/0/0"],
"update": {
"chassis": {
"serial": {"old_value": "OLD123", "new_value": "JCY98XR393D"}
}
},
"delete": ["module stale"],
"in_sync": []
}
}
Live-run mode (dry_run=False, default) applies changes and returns a
per-device action summary:
{
"<device>": {
"created": [
"Cisco A9K-RSP440-TR",
"Cisco SFP-10G-LR",
"module 0/RSP0/CPU0",
"module mau 0/1/0/0"
],
"updated": ["chassis"],
"deleted": [],
"in_sync": []
}
}
The created list includes created module bays, module types, and installed
modules. In live-run mode res["diff"] is also populated with the raw diff.
Missing module bays, missing module types, failed writes, and ignored live
records are reported in res["errors"].
Examples¤
Preview chassis serial and module changes for one device:
nf#netbox sync device-inventory devices iosxr1 dry-run
Sync a device using only existing NetBox module bays and module types:
nf#netbox sync device-inventory devices iosxr1
Create missing module bays from live slot names, but require module types to already exist:
nf#netbox sync device-inventory devices iosxr1 create-module-bays
Create missing module bays and module types, then install modules:
nf#netbox sync device-inventory devices iosxr1 create-module-bays create-module-types
Sync multiple devices:
nf#netbox sync device-inventory devices iosxr1 iosxr2 create-module-bays create-module-types
Delete stale NetBox modules that are absent from live inventory:
nf#netbox sync device-inventory devices iosxr1 create-module-bays create-module-types process-deletions
Sync into a NetBox branch:
nf#netbox sync device-inventory devices iosxr1 branch inventory-sync-branch create-module-bays create-module-types
Add a NetBox changelog message to write operations:
nf#netbox sync device-inventory devices iosxr1 message "sync inventory from live device" create-module-bays create-module-types
Use Nornir host filters instead of explicit device names:
nf#netbox sync device-inventory FC iosxr create-module-bays create-module-types
Target a specific NetBox worker and keep detailed output:
nf#netbox sync device-inventory workers netbox-worker-1 devices iosxr1 verbose-result
Normalize parsed inventory with a Python transformer:
nf#netbox sync device-inventory devices iosxr1 inventory-transform nf://netbox/inventory_transformers/iosxr.py dry-run
Load inventory mappings from a YAML file:
nf#netbox sync device-inventory devices iosxr1 inventory-map nf://netbox/inventory_maps/iosxr.yaml dry-run
Sync only RSP and line-card modules in slots below module 0:
nf#netbox sync device-inventory devices iosxr1 filter-by-module "A9K-RSP*" "A9K-MOD*" filter-by-slot "module 0/*" dry-run
Ignore optics and power-module slots:
nf#netbox sync device-inventory devices iosxr1 ignore-modules "SFP-*" ignore-slots "power-module *" dry-run
from norfab.core.nfapi import NorFab
nf = NorFab(inventory="./inventory.yaml")
nf.start()
client = nf.make_client()
# dry run - preview raw create/update/delete/in_sync diff
result = client.run_job(
"netbox",
"sync_device_inventory",
workers="any",
kwargs={
"devices": ["iosxr1"],
"dry_run": True,
},
)
# sync using existing NetBox module bays and module types only
result = client.run_job(
"netbox",
"sync_device_inventory",
workers="any",
kwargs={
"devices": ["iosxr1"],
},
)
# create missing module bays from live inventory slot names
result = client.run_job(
"netbox",
"sync_device_inventory",
workers="any",
kwargs={
"devices": ["iosxr1"],
"create_module_bays": True,
},
)
# create missing module bays and module types, then install modules
result = client.run_job(
"netbox",
"sync_device_inventory",
workers="any",
kwargs={
"devices": ["iosxr1"],
"create_module_bays": True,
"create_module_types": True,
},
)
# sync multiple devices
result = client.run_job(
"netbox",
"sync_device_inventory",
workers="any",
kwargs={
"devices": ["iosxr1", "iosxr2"],
"create_module_bays": True,
"create_module_types": True,
},
)
# delete stale NetBox modules absent from live inventory
result = client.run_job(
"netbox",
"sync_device_inventory",
workers="any",
kwargs={
"devices": ["iosxr1"],
"create_module_bays": True,
"create_module_types": True,
"process_deletions": True,
},
)
# sync into a NetBox branch and attach a changelog message
result = client.run_job(
"netbox",
"sync_device_inventory",
workers="any",
kwargs={
"devices": ["iosxr1"],
"branch": "inventory-sync-branch",
"message": "sync inventory from live device",
"create_module_bays": True,
"create_module_types": True,
},
)
# use Nornir host filters instead of explicit device names
result = client.run_job(
"netbox",
"sync_device_inventory",
workers="any",
kwargs={
"FC": "iosxr",
"create_module_bays": True,
"create_module_types": True,
},
)
# filter and ignore normalized module type and slot names
result = client.run_job(
"netbox",
"sync_device_inventory",
workers="any",
kwargs={
"devices": ["iosxr1"],
"filter_by_module": ["A9K-RSP*", "A9K-MOD*"],
"filter_by_slot": ["module 0/*"],
"ignore_modules": ["*-BUILTIN"],
"ignore_slots": ["module 0/PS*"],
"dry_run": True,
},
)
# map live module and slot names to existing NetBox names
inventory_map = {
"module_types": {
"Cisco": {
"ASR 9000 RSP440": [
{"glob": "A9K-RSP440-*"},
],
"10GBASE-LR SFP+": [
{"regex": r"^SFP-10G-LR(=)?$"},
],
},
},
"module_bays": {
"Cisco": {
"ASR-9006": {
"0/RSP0": [
{"glob": "module 0/RSP0/*"},
],
"TenGigE0/2/CPU0/0": [
{
"eval": (
"value == "
"'module mau TenGigE0/2/CPU0/0'"
)
},
],
},
},
},
}
result = client.run_job(
"netbox",
"sync_device_inventory",
workers="any",
kwargs={
"devices": ["iosxr1"],
"inventory_map": inventory_map,
"dry_run": True,
},
)
# load the same mapping structure from the NorFab File Service
result = client.run_job(
"netbox",
"sync_device_inventory",
workers="any",
kwargs={
"devices": ["iosxr1"],
"inventory_map": "nf://netbox/inventory_maps/iosxr.yaml",
"dry_run": True,
},
)
# run a transformer, then apply mappings to its returned records
result = client.run_job(
"netbox",
"sync_device_inventory",
workers="any",
kwargs={
"devices": ["iosxr1"],
"inventory_transform": (
"nf://netbox/inventory_transformers/iosxr.py"
),
"inventory_map": inventory_map,
"dry_run": True,
},
)
nf.destroy()
Inline inventory_map data is most conveniently supplied through the Python
API or workflow task kwargs. NFCLI supports the file-backed inventory-map
and inventory-transform references shown above.
NORFAB Netbox Sync Device Inventory Command Shell Reference¤
NorFab shell supports these command options for the NetBox
sync_device_inventory task:
nf# man tree netbox.sync.device-inventory
root
└── netbox: Netbox service
└── sync: Sync Netbox data
└── device-inventory: Sync device inventory facts e.g. serial number
├── timeout: Job timeout
├── workers: Filter worker to target, default 'any'
├── verbose-result: Control output details, default 'False'
├── nowait: Do not wait for job to complete, default 'False'
├── instance: Netbox instance name to target
├── dry-run: Do not commit to database
├── branch: NetBox branching plugin branch name to use
├── devices: List of NetBox devices to sync inventory for
├── process-deletions: Delete NetBox modules present in module bays but absent from live inventory
├── create-module-types: Create missing NetBox module types from live inventory model data
├── create-module-bays: Create missing NetBox module bays using the live inventory slot names
├── inventory-map: Pattern mappings or nf:// YAML file reference
├── inventory-transform: nf:// Python transformer file containing a transform function
├── filter-by-module: Glob patterns selecting normalized module type names
├── filter-by-slot: Glob patterns selecting normalized module bay names
├── ignore-modules: Glob patterns excluding normalized module type names
├── ignore-slots: Glob patterns excluding normalized module bay names
├── message: Changelog message recorded on NetBox writes
├── FO: Filter hosts using Filter Object
├── FB: Filter hosts by name using Glob Patterns
├── FH: Filter hosts by hostname
├── FC: Filter hosts containment of pattern in name
├── FR: Filter hosts by name using Regular Expressions
├── FG: Filter hosts by group
├── FP: Filter hosts by hostname using IP Prefix
├── FL: Filter hosts by names list
├── FM: Filter hosts by platform
├── FX: Filter hosts excluding them by name
└── FN: Negate the match
nf#
Python API Reference¤
Synchronize chassis serial and installed module inventory into NetBox.
Live inventory is collected from the Nornir service using
parse_ttp(get="inventory"). The chassis record updates the NetBox
device serial. All non-chassis records with a usable module identity
and serial are reconciled as NetBox modules installed in device-level
module bays.
Module deletions are disabled by default. Missing module bays and
module types are reported unless explicitly created with
create_module_bays or create_module_types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
NorFab job object used for progress events. |
required |
instance
|
Union[None, str]
|
NetBox instance name to target. Defaults to the worker's default NetBox instance. |
None
|
dry_run
|
bool
|
Return the planned inventory diff without writing changes to NetBox. |
False
|
timeout
|
int
|
Timeout in seconds for the Nornir |
60
|
devices
|
Union[None, list]
|
NetBox device names to synchronize. |
None
|
branch
|
str
|
NetBox Branching plugin branch name. |
None
|
process_deletions
|
bool
|
Delete stale NetBox modules when they are absent from live inventory. |
False
|
create_module_types
|
bool
|
Create missing NetBox module types from live module model data. |
False
|
create_module_bays
|
bool
|
Create missing NetBox module bays from live inventory slot names. |
False
|
inventory_map
|
Union[None, str, dict, InventoryPatternMap]
|
Manufacturer and device type scoped pattern mappings,
or an |
None
|
inventory_transform
|
Union[None, str]
|
|
None
|
filter_by_module
|
Union[None, list]
|
Glob patterns selecting normalized module type names. |
None
|
filter_by_slot
|
Union[None, list]
|
Glob patterns selecting normalized module bay names. |
None
|
ignore_modules
|
Union[None, list]
|
Glob patterns excluding normalized module type names. |
None
|
ignore_slots
|
Union[None, list]
|
Glob patterns excluding normalized module bay names. |
None
|
message
|
Union[None, str]
|
NetBox changelog message to attach to write operations. |
None
|
**kwargs
|
Any
|
Additional Nornir filters and execution options forwarded
to |
{}
|
Returns:
| Type | Description |
|---|---|
Result
|
Result object containing the normalized diff, per-device sync |
Result
|
outcome, and any partial errors. |
Source code in norfab\workers\netbox_worker\devices_tasks.py
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 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 | |