Skip to content

Simple Inventory

Simple Local Inventory is an inventory plugin to load inventory data from locally stored files.

Sample inventory file

broker:
  endpoint: "tcp://127.0.0.1:5555"

workers:
  nornir-*:
    - nornir/common.yaml  
  nornir-worker-1:
    - nornir/nornir-worker-1.yaml

topology:
  broker: True
  workers:
    - nornir-worker-1

where nornir/common.yaml contains

service: nornir
broker_endpoint: "tcp://127.0.0.1:5555"
runner:
  plugin: RetryRunner
  options: 
    num_workers: 100
    num_connectors: 10
    connect_retry: 3
    connect_backoff: 1000
    connect_splay: 100
    task_retry: 3
    task_backoff: 1000
    task_splay: 100
    reconnect_on_fail: True
    task_timeout: 600

and nornir/nornir-worker-1.yaml contains

hosts: 
  csr1000v-1:
    hostname: sandbox-1.lab.com
    platform: cisco_ios
    username: developer
    password: secretpassword
  csr1000v-2:
    hostname: sandbox-2.lab.com
    platform: cisco_ios
    username: developer
    password: secretpassword
groups: {}
defaults: {}

Whenever inventory queried to provide data for worker with name nornir-worker-1 Simple Inventory iterates over workers dictionary and recursively merges data for keys (glob patterns) that matched worker name.

WorkersInventory(path, data) ¤

Class to collect and server NorFab workers inventory data, forming it by recursively merging all data files that associated with the name of worker requesting inventory data.

Parameters:

Name Type Description Default
path str

OS path to top folder with workers inventory data

required
data dict

dictionary keyed by glob patterns matching workers names and values being a list of OS paths to files with workers inventory data

required
Source code in norfab\core\inventory.py
108
109
110
111
112
113
114
115
116
117
118
119
120
def __init__(self, path: str, data: dict) -> None:
    """
    Class to collect and server NorFab workers inventory data,
    forming it by recursively merging all data files that associated
    with the name of worker requesting inventory data.

    :param path: OS path to top folder with workers inventory data
    :param data: dictionary keyed by glob patterns matching workers names
        and values being a list of OS paths to files with workers
        inventory data
    """
    self.path, _ = os.path.split(path)
    self.data = data

NorFabInventory(path) ¤

NorFabInventory class to instantiate simple inventory.

Parameters:

Name Type Description Default
path str

OS path to YAML file with inventory data

required
Source code in norfab\core\inventory.py
151
152
153
154
155
156
157
158
159
160
161
def __init__(self, path: str) -> None:
    """
    NorFabInventory class to instantiate simple inventory.

    :param path: OS path to YAML file with inventory data
    """
    self.broker = {}
    self.workers = {}
    self.topology = {}
    path = os.path.abspath(path)
    self.load(path)

merge_recursively(data, merge) ¤

Function to merge two dictionaries data recursively.

Parameters:

Name Type Description Default
data dict

primary dictionary

required
merge dict

dictionary to merge into primary overriding the content

required
Source code in norfab\core\inventory.py
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
def merge_recursively(data: dict, merge: dict) -> None:
    """
    Function to merge two dictionaries data recursively.

    :param data: primary dictionary
    :param merge: dictionary to merge into primary overriding the content
    """
    assert isinstance(data, dict) and isinstance(
        merge, dict
    ), f"Only supports dictionary/dictionary data merges, not {type(data)}/{type(merge)}"
    for k, v in merge.items():
        if k in data:
            # merge two lists
            if isinstance(data[k], list) and isinstance(v, list):
                for i in v:
                    if i not in data[k]:
                        data[k].append(i)
            # recursively merge dictionaries
            elif isinstance(data[k], dict) and isinstance(v, dict):
                merge_recursively(data[k], v)
            # rewrite existing value with new data
            else:
                data[k] = v
        else:
            data[k] = v