Netbox Worker
NetboxWorker(inventory, broker, worker_name, exit_event=None, init_done_event=None, log_level: str = None, log_queue: object = None)
¤
Bases: NFPWorker, NetboxGraphqlTasks, NetboxDesignTasks, NetboxInterfacesTasks, NetboxDevicesTasks, NetboxConnectionsTasks, NetboxCircuitsTasks, NetboxNornirInventoryTasks, NetboxBgpPeeringsTasks, NetboxPrefixTasks, NetboxContainerlabInventoryTasks, NetboxIpTasks, NetboxBranchTasks, NetboxCrudTasks
NetboxWorker class for interacting with Netbox API and managing inventory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inventory
|
dict
|
The inventory data. |
required |
broker
|
object
|
The broker instance. |
required |
worker_name
|
str
|
The name of the worker. |
required |
exit_event
|
Event
|
Event to signal exit. |
None
|
init_done_event
|
Event
|
Event to signal initialization completion. |
None
|
log_level
|
int
|
Logging level. |
None
|
log_queue
|
object
|
Queue for logging. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the inventory has no Netbox instances. |
Attributes:
| Name | Type | Description |
|---|---|---|
default_instance |
str
|
Default Netbox instance name. |
inventory |
dict
|
Inventory data. |
nb_version |
tuple
|
Netbox version. |
compatible_ge_v4 |
tuple
|
Minimum supported Netbox v4 version (4.4.0+). |
Source code in norfab\workers\netbox_worker\netbox_worker.py
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 | |
worker_exit() -> None
¤
Worker exist sanity checks. Closes the cache if it exists.
This method checks if the cache attribute is present and not None. If the cache exists, it closes the cache to release any resources associated with it.
Source code in norfab\workers\netbox_worker\netbox_worker.py
148 149 150 151 152 153 154 155 156 157 | |
get_inventory() -> Result
¤
NorFab Task to return running inventory for NetBox worker.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Result
|
A dictionary containing the NetBox inventory. |
Source code in norfab\workers\netbox_worker\netbox_worker.py
163 164 165 166 167 168 169 170 171 172 173 | |
get_version(**kwargs: Any) -> Result
¤
Retrieves the version information of Netbox instances.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Result
|
A dictionary containing the version information of the Netbox |
Source code in norfab\workers\netbox_worker\netbox_worker.py
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 | |
get_netbox_status(instance: Union[None, str] = None) -> Result
¤
Retrieve the status of NetBox instances.
This method queries the status of a specific NetBox instance if the
instance parameter is provided. If no instance is specified, it
queries the status of all instances in the NetBox inventory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
str
|
The name of the specific NetBox instance to query. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Result
|
A dictionary containing the status of the requested NetBox instance(s). |
Source code in norfab\workers\netbox_worker\netbox_worker.py
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 | |
get_compatibility(job: Job) -> Result
¤
Checks the compatibility of Netbox instances based on their version.
This method retrieves the status and version of Netbox instances and determines if they are compatible with the required versions. It logs a warning if any instance is not reachable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
NorFab Job object containing relevant metadata |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Result
|
A dictionary where the keys are the instance names and the values are booleans indicating compatibility (True/False) or None if the instance is not reachable. |
Source code in norfab\workers\netbox_worker\netbox_worker.py
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 | |
has_plugin(plugin_name: str, instance: str, strict: bool = False) -> bool
¤
Check if a specified plugin is installed in a given NetBox instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plugin_name
|
str
|
The name of the plugin to check for. |
required |
instance
|
str
|
The identifier or address of the NetBox instance. |
required |
strict
|
bool
|
If True, raises a RuntimeError when the plugin is not found. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the plugin is installed, False otherwise. |
Source code in norfab\workers\netbox_worker\netbox_worker.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
make_diff(source_data: dict, target_data: dict) -> dict
¤
Compute an actionable diff between two nested dictionaries and classify
each entity as create, delete, update, or in_sync.
Both arguments share the same two-level structure: the outer key typically is a device name and the inner key is a unique entity identifier (name, slug, or any hashable value). The inner value is a flat dict of comparable fields:
{
"<device_name>": {
"<entity_id>": {<field>: <value>, ...},
}
}
source_data represents the desired or discovered state (e.g. live
device data), while target_data represents the current state stored
in the target system (e.g. NetBox). Entities present in source_data
but absent from target_data are classified as create; entities
present in target_data but absent from source_data are classified
as delete; entities present in both with differing field values are
classified as update; identical entities are in_sync.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_data
|
dict
|
Nested dict representing the discovered/live state. Outer key is the device name; inner key is the entity identifier; value is a flat dict of entity fields. |
required |
target_data
|
dict
|
Nested dict representing the desired/managed state.
Same structure as |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Keyed by group name, each value contains: |
dict
|
|
|
dict
|
|
|
dict
|
|
|
dict
|
|
Source code in norfab\workers\netbox_worker\netbox_worker.py
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 | |
cache_list(keys: str = '*', details: bool = False) -> Result
¤
Retrieve a list of cache keys, optionally with details about each key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str
|
A pattern to match cache keys against. Defaults to "*". |
'*'
|
details
|
bool
|
If True, include detailed information about each cache key. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
Result
|
A list of cache keys or a list of dictionaries with detailed information if |
Source code in norfab\workers\netbox_worker\netbox_worker.py
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 | |
cache_clear(job: Job, key: str = None, keys: str = None) -> Result
¤
Clears specified keys from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
NorFab Job object containing relevant metadata |
required |
key
|
str
|
A specific key to remove from the cache. |
None
|
keys
|
str
|
A glob pattern to match multiple keys to remove from the cache. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
Result
|
A list of keys that were successfully removed from the cache. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If a specified key or a key matching the glob pattern could not be removed from the cache. |
Notes:
- If neither
keynorkeysis provided, the function will return a message indicating that there is nothing to clear. - If
keyis provided, it will attempt to remove that specific key from the cache. - If
keysis provided, it will attempt to remove all keys matching the glob pattern from the cache.
Source code in norfab\workers\netbox_worker\netbox_worker.py
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 | |
cache_get(job: Job, key: str = None, keys: str = None, raise_missing: bool = False) -> Result
¤
Retrieve values from the cache based on a specific key or a pattern of keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
NorFab Job object containing relevant metadata |
required |
key
|
str
|
A specific key to retrieve from the cache. |
None
|
keys
|
str
|
A glob pattern to match multiple keys in the cache. |
None
|
raise_missing
|
bool
|
If True, raises a KeyError if the specific key is not found in the cache. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Result
|
A dictionary containing the results of the cache retrieval. The keys are the cache keys and the values are the corresponding cache values. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If raise_missing is True and the specific key is not found in the cache. |
Source code in norfab\workers\netbox_worker\netbox_worker.py
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 | |
rest(job: Job, instance: Union[None, str] = None, method: str = 'get', api: str = '', **kwargs: Any) -> Result
¤
Sends a request to the Netbox REST API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
str
|
The Netbox instance name to get parameters for. |
None
|
method
|
str
|
The HTTP method to use for the request (e.g., 'get', 'post'). Defaults to "get". |
'get'
|
api
|
str
|
The API endpoint to send the request to. Defaults to "". |
''
|
**kwargs
|
Any
|
Additional arguments to pass to the request (e.g., params, data, json). |
{}
|
Returns:
| Type | Description |
|---|---|
Result
|
Union[dict, list]: The JSON response from the API, parsed into a dictionary or list. |
Raises:
| Type | Description |
|---|---|
HTTPError
|
If the HTTP request returned an unsuccessful status code. |
Source code in norfab\workers\netbox_worker\netbox_worker.py
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 | |
compare_netbox_object_state(desired_state: dict, current_state: dict, ignore_fields: Union[list, None] = None, ignore_if_not_empty: Union[list, None] = None, diff: dict = None) -> tuple
¤
Compare desired state with current NetBox object state and return fields that need updating.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desired_state
|
dict
|
Dictionary with desired field values. |
required |
current_state
|
dict
|
Dictionary with current NetBox object field values. |
required |
ignore_fields
|
list
|
List of field names to ignore completely. |
None
|
ignore_if_not_empty
|
list
|
List of field names to ignore if they have non-empty values in current_state (won't overwrite existing data). |
None
|
diff
|
dict
|
Dictionary to accumulate field differences. If not provided, a new dictionary will be created. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple
|
A tuple containing: - updates (dict): Dictionary containing only fields that need to be updated with their new values. - diff (dict): Dictionary containing the differences with '+' (new value) and '-' (old value) keys. |
Example
desired = {"serial": "ABC123", "asset_tag": "TAG001", "comments": "New comment"} current = {"serial": "OLD123", "asset_tag": "", "comments": "Existing"} ignore_fields = [] ignore_if_not_empty = ["comments"] updates, diff = compare_netbox_object_state(desired, current, ignore_fields, ignore_if_not_empty) updates
Source code in norfab\workers\netbox_worker\netbox_worker.py
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 | |
get_nornir_hosts(kwargs: dict, timeout: int) -> List[str]
¤
Retrieves a list of unique Nornir hosts from Nornir service based on provided filter criteria.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
dict
|
Dictionary of keyword arguments, where keys starting with 'F' are used as filters. |
required |
timeout
|
int
|
Timeout value (in seconds) for the job execution. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
List[str]
|
Sorted list of unique Nornir host names that match the filter criteria. |
Notes
- Only filters with keys starting with 'F' are considered.
- Hosts are collected from all workers where the job did not fail.
Source code in norfab\workers\netbox_worker\netbox_worker.py
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 | |