Skip to content

Nornir Worker

WatchDog(worker) ¤

Bases: WorkerWatchDog

Class to monitor Nornir worker performance.

Parameters:

Name Type Description Default
worker Worker

The worker instance that this NornirWorker will manage.

required

Attributes:

Name Type Description
worker Worker

The worker instance being monitored.

connections_idle_timeout int

Timeout value for idle connections.

connections_data dict

Dictionary to store connection use timestamps.

started_at float

Timestamp when the watchdog was started.

idle_connections_cleaned int

Counter for idle connections cleaned.

dead_connections_cleaned int

Counter for dead connections cleaned.

watchdog_tasks list

List of tasks for the watchdog to run in a given order.

Source code in norfab\workers\nornir_worker.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def __init__(self, worker):
    super().__init__(worker)
    self.worker = worker
    self.connections_idle_timeout = worker.inventory.get(
        "connections_idle_timeout", None
    )
    self.connections_data = {}  # store connections use timestamps
    self.started_at = time.time()

    # stats attributes
    self.idle_connections_cleaned = 0
    self.dead_connections_cleaned = 0

    # list of tasks for watchdog to run in given order
    self.watchdog_tasks = [
        self.connections_clean,
        self.connections_keepalive,
    ]

stats() -> Dict ¤

Collects and returns statistics about the worker.

Returns:

Name Type Description
dict Dict

A dictionary containing the following keys:

  • runs (int): The number of runs executed by the worker.
  • timestamp (str): The current time in a human-readable format.
  • alive (int): The time in seconds since the worker started.
  • dead_connections_cleaned (int): The number of dead connections cleaned.
  • idle_connections_cleaned (int): The number of idle connections cleaned.
  • worker_ram_usage_mbyte (float): The current RAM usage of the worker in megabytes.
Source code in norfab\workers\nornir_worker.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def stats(self) -> Dict:
    """
    Collects and returns statistics about the worker.

    Returns:
        dict: A dictionary containing the following keys:

            - runs (int): The number of runs executed by the worker.
            - timestamp (str): The current time in a human-readable format.
            - alive (int): The time in seconds since the worker started.
            - dead_connections_cleaned (int): The number of dead connections cleaned.
            - idle_connections_cleaned (int): The number of idle connections cleaned.
            - worker_ram_usage_mbyte (float): The current RAM usage of the worker in megabytes.
    """
    return {
        "runs": self.runs,
        "timestamp": time.ctime(),
        "alive": int(time.time() - self.started_at),
        "dead_connections_cleaned": self.dead_connections_cleaned,
        "idle_connections_cleaned": self.idle_connections_cleaned,
        "worker_ram_usage_mbyte": self.get_ram_usage(),
    }

configuration() -> Dict ¤

Returns the configuration settings for the worker.

Returns:

Name Type Description
Dict Dict

A dictionary containing the configuration settings:

  • "watchdog_interval" (int): The interval for the watchdog timer.
  • "connections_idle_timeout" (int): The timeout for idle connections.
Source code in norfab\workers\nornir_worker.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def configuration(self) -> Dict:
    """
    Returns the configuration settings for the worker.

    Returns:
        Dict: A dictionary containing the configuration settings:

            - "watchdog_interval" (int): The interval for the watchdog timer.
            - "connections_idle_timeout" (int): The timeout for idle connections.
    """
    return {
        "watchdog_interval": self.watchdog_interval,
        "connections_idle_timeout": self.connections_idle_timeout,
    }

connections_get() -> Dict ¤

Retrieve the current connections data.

Returns:

Name Type Description
Dict Dict

A dictionary containing the current connections data.

Source code in norfab\workers\nornir_worker.py
132
133
134
135
136
137
138
139
140
141
def connections_get(self) -> Dict:
    """
    Retrieve the current connections data.

    Returns:
        Dict: A dictionary containing the current connections data.
    """
    return {
        "connections": self.connections_data,
    }

connections_update(nr, plugin: str) -> None ¤

Function to update connection use timestamps for each host

Parameters:

Name Type Description Default
nr

Nornir object

required
plugin str

connection plugin name

required
Source code in norfab\workers\nornir_worker.py
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def connections_update(self, nr, plugin: str) -> None:
    """
    Function to update connection use timestamps for each host

    Args:
        nr: Nornir object
        plugin: connection plugin name
    """
    conn_stats = {
        "last_use": None,
        "last_keepealive": None,
        "keepalive_count": 0,
    }
    for host_name in nr.inventory.hosts:
        self.connections_data.setdefault(host_name, {})
        self.connections_data[host_name].setdefault(plugin, conn_stats.copy())
        self.connections_data[host_name][plugin]["last_use"] = time.ctime()
    log.info(
        f"{self.worker.name} - updated connections use timestamps for '{plugin}'"
    )

connections_clean() ¤

Cleans up idle connections based on the configured idle timeout.

This method checks for connections that have been idle for longer than the specified connections_idle_timeout and disconnects them. The behavior varies depending on the value of connections_idle_timeout:

  • If connections_idle_timeout is None, no connections are disconnected.
  • If connections_idle_timeout is 0, all connections are disconnected.
  • If connections_idle_timeout is greater than 0, only connections that have been idle for longer than the specified timeout are disconnected.

The method acquires a lock to ensure thread safety while modifying the connections data. It logs the disconnection actions and updates the idle_connections_cleaned counter.

Raises:

Type Description
Exception

If an error occurs while attempting to disconnect idle

Source code in norfab\workers\nornir_worker.py
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
def connections_clean(self):
    """
    Cleans up idle connections based on the configured idle timeout.

    This method checks for connections that have been idle for longer than the
    specified `connections_idle_timeout` and disconnects them. The behavior
    varies depending on the value of `connections_idle_timeout`:

    - If `connections_idle_timeout` is None, no connections are disconnected.
    - If `connections_idle_timeout` is 0, all connections are disconnected.
    - If `connections_idle_timeout` is greater than 0, only connections that
      have been idle for longer than the specified timeout are disconnected.

    The method acquires a lock to ensure thread safety while modifying the
    connections data. It logs the disconnection actions and updates the
    `idle_connections_cleaned` counter.

    Raises:
        Exception: If an error occurs while attempting to disconnect idle
        connections, an error message is logged.
    """
    # dictionary keyed by plugin name and value as a list of hosts
    disconnect = {}
    if not self.worker.connections_lock.acquire(blocking=False):
        return
    try:
        # if idle timeout not set, connections don't age out
        if self.connections_idle_timeout is None:
            disconnect = {}
        # disconnect all connections for all hosts
        elif self.connections_idle_timeout == 0:
            disconnect = {"all": list(self.connections_data.keys())}
        # only disconnect aged/idle connections
        elif self.connections_idle_timeout > 0:
            for host_name, plugins in self.connections_data.items():
                for plugin, conn_data in plugins.items():
                    last_use = time.mktime(time.strptime(conn_data["last_use"]))
                    age = time.time() - last_use
                    if age > self.connections_idle_timeout:
                        disconnect.setdefault(plugin, [])
                        disconnect[plugin].append(host_name)
        # run task to disconnect connections for aged hosts
        for plugin, hosts in disconnect.items():
            if not hosts:
                continue
            aged_hosts = FFun(self.worker.nr, FL=hosts)
            aged_hosts.run(task=nr_connections, call="close", conn_name=plugin)
            log.debug(
                f"{self.worker.name} watchdog, disconnected '{plugin}' "
                f"connections for '{', '.join(hosts)}'"
            )
            self.idle_connections_cleaned += len(hosts)
            # wipe out connections data if all connection closed
            if plugin == "all":
                self.connections_data = {}
                break
            # remove disconnected plugin from host's connections_data
            for host in hosts:
                self.connections_data[host].pop(plugin)
                if not self.connections_data[host]:
                    self.connections_data.pop(host)
    except Exception as e:
        msg = f"{self.worker.name} - watchdog failed to close idle connections, error: {e}"
        log.error(msg)
    finally:
        self.worker.connections_lock.release()

connections_keepalive() ¤

Keepalive connections and clean up dead connections if any.

This method performs the following tasks:

  • If connections_idle_timeout is 0, it returns immediately without performing any actions.
  • Attempts to acquire a lock on worker.connections_lock to ensure thread safety.
  • Logs a debug message indicating that the keepalive process is running.
  • Uses HostsKeepalive to check and clean up dead connections, updating the dead_connections_cleaned counter.
  • Removes connections that are no longer present in the Nornir inventory.
  • Removes hosts from connections_data if they have no remaining connections.
  • Updates the keepalive statistics for each connection plugin, including the last keepalive time and keepalive count.
  • Logs an error message if an exception occurs during the keepalive process.
  • Releases the lock on worker.connections_lock in the finally block to ensure it is always released.

Raises:

Type Description
Exception

If an error occurs during the keepalive process, it is logged as an error.

Source code in norfab\workers\nornir_worker.py
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
def connections_keepalive(self):
    """
    Keepalive connections and clean up dead connections if any.

    This method performs the following tasks:

    - If `connections_idle_timeout` is 0, it returns immediately without performing any actions.
    - Attempts to acquire a lock on `worker.connections_lock` to ensure thread safety.
    - Logs a debug message indicating that the keepalive process is running.
    - Uses `HostsKeepalive` to check and clean up dead connections, updating the `dead_connections_cleaned` counter.
    - Removes connections that are no longer present in the Nornir inventory.
    - Removes hosts from `connections_data` if they have no remaining connections.
    - Updates the keepalive statistics for each connection plugin, including the last keepalive time and keepalive count.
    - Logs an error message if an exception occurs during the keepalive process.
    - Releases the lock on `worker.connections_lock` in the `finally` block to ensure it is always released.

    Raises:
        Exception: If an error occurs during the keepalive process, it is logged as an error.
    """
    if self.connections_idle_timeout == 0:  # do not keepalive if idle is 0
        return
    if not self.worker.connections_lock.acquire(blocking=False):
        return
    try:
        log.debug(f"{self.worker.name} - watchdog running connections keepalive")
        stats = HostsKeepalive(self.worker.nr)
        self.dead_connections_cleaned += stats["dead_connections_cleaned"]
        # remove connections that are no longer present in Nornir inventory
        for host_name, host_connections in self.connections_data.items():
            for connection_name in list(host_connections.keys()):
                if not self.worker.nr.inventory.hosts[host_name].connections.get(
                    connection_name
                ):
                    self.connections_data[host_name].pop(connection_name)
        # remove host if no connections left
        for host_name in list(self.connections_data.keys()):
            if self.connections_data[host_name] == {}:
                self.connections_data.pop(host_name)
        # update connections statistics
        for plugins in self.connections_data.values():
            for plugin in plugins.values():
                plugin["last_keepealive"] = time.ctime()
                plugin["keepalive_count"] += 1
    except Exception as e:
        msg = f"{self.worker.name} - watchdog HostsKeepalive check error: {e}"
        log.error(msg)
    finally:
        self.worker.connections_lock.release()

NornirWorker(inventory: str, broker: str, worker_name: str, exit_event=None, init_done_event=None, log_level: str = None, log_queue: object = None) ¤

Bases: NFPWorker

NornirWorker class for managing Nornir Service tasks.

Parameters:

Name Type Description Default
inventory str

Path to the inventory file.

required
broker str

Broker address.

required
worker_name str

Name of the worker.

required
exit_event Event

Event to signal worker exit. Defaults to None.

None
init_done_event Event

Event to signal initialization completion. Defaults to None.

None
log_level str

Logging level. Defaults to None.

None
log_queue object

Queue for logging. Defaults to None.

None

Attributes:

Name Type Description
init_done_event Event

Event to signal initialization completion.

tf_base_path str

Base path for files folder saved using tf processor.

connections_lock Lock

Lock for managing connections.

nornir_inventory dict

Inventory data for Nornir.

watchdog WatchDog

Watchdog instance for monitoring.

Source code in norfab\workers\nornir_worker.py
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
def __init__(
    self,
    inventory: str,
    broker: str,
    worker_name: str,
    exit_event=None,
    init_done_event=None,
    log_level: str = None,
    log_queue: object = None,
):
    super().__init__(
        inventory, broker, SERVICE, worker_name, exit_event, log_level, log_queue
    )
    self.init_done_event = init_done_event
    self.tf_base_path = os.path.join(self.base_dir, "tf")

    # misc attributes
    self.connections_lock = Lock()

    # get inventory from broker
    self.nornir_inventory = self.load_inventory()

    # pull Nornir inventory from Netbox
    self._pull_netbox_inventory()

    # initiate Nornir
    self._init_nornir()

    # initiate watchdog
    self.watchdog = WatchDog(self)
    self.watchdog.start()

    # run startup hooks
    for f in self.inventory.hooks.get("nornir-startup", []):
        f["function"](self, *f.get("args", []), **f.get("kwargs", {}))

    if self.init_done_event is not None:
        self.init_done_event.set()

    log.info(f"{self.name} - Started")

worker_exit() ¤

Executes all functions registered under the "nornir-exit" hook in the inventory.

This method iterates through the list of hooks associated with the "nornir-exit" key in the inventory's hooks.

For each hook, it calls the function specified in the hook, passing the current instance (self) as the first argument, followed by any additional positional and keyword arguments specified in the hook.

Source code in norfab\workers\nornir_worker.py
343
344
345
346
347
348
349
350
351
352
353
354
355
356
def worker_exit(self):
    """
    Executes all functions registered under the "nornir-exit" hook in the inventory.

    This method iterates through the list of hooks associated with the "nornir-exit"
    key in the inventory's hooks.

    For each hook, it calls the function specified in the hook, passing the current
    instance (`self`) as the first argument, followed by any additional positional
    and keyword arguments specified in the hook.
    """
    # run exit hooks
    for f in self.inventory.hooks.get("nornir-exit", []):
        f["function"](self, *f.get("args", []), **f.get("kwargs", {}))

load_job_data(job_data: str) ¤

Helper function to download job data YAML files and load it.

Parameters:

Name Type Description Default
job_data str

job data NorFab file path to download and load using YAML.

required

Returns:

Name Type Description
data

The job data loaded from the YAML string.

Raises:

Type Description
FileNotFoundError

If the job data is a URL and the file download fails.

Source code in norfab\workers\nornir_worker.py
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
def load_job_data(self, job_data: str):
    """
    Helper function to download job data YAML files and load it.

    Args:
        job_data (str): job data NorFab file path to download and load using YAML.

    Returns:
        data: The job data loaded from the YAML string.

    Raises:
        FileNotFoundError: If the job data is a URL and the file download fails.
    """
    if self.is_url(job_data):
        job_data = self.fetch_file(job_data)
        if job_data is None:
            msg = f"{self.name} - '{job_data}' job data file download failed"
            raise FileNotFoundError(msg)
        job_data = yaml.safe_load(job_data)

    return job_data

add_jinja2_filters() -> Dict ¤

Adds custom filters for use in Jinja2 templates.

Returns:

Name Type Description
dict Dict

A dictionary where the keys are the names of the filters and the values are the corresponding filter functions.

  • "nb_get_next_ip": Method to get the next IP address.
  • "network_hosts": Method to get IP network hosts.
Source code in norfab\workers\nornir_worker.py
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
def add_jinja2_filters(self) -> Dict:
    """
    Adds custom filters for use in Jinja2 templates.

    Returns:
        dict (Dict): A dictionary where the keys are the names of the filters
            and the values are the corresponding filter functions.

            - "nb_get_next_ip": Method to get the next IP address.
            - "network_hosts": Method to get IP network hosts.
    """
    return {
        "nb_get_next_ip": self.nb_get_next_ip,
        "network_hosts": self._jinja2_network_hosts,
    }

get_nornir_hosts(details: bool = False, **kwargs: dict) -> List[Union[str, Dict]] ¤

Retrieve a list of Nornir hosts managed by this worker.

Parameters:

Name Type Description Default
details bool

If True, returns detailed information about each host.

False
**kwargs dict

Hosts filters to apply when retrieving hosts.

{}

Returns:

Type Description
List[Union[str, Dict]]

List[Dict]: A list of hosts with optional detailed information.

Source code in norfab\workers\nornir_worker.py
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
@Task(input=GetNornirHosts, output=GetNornirHostsResponse)
def get_nornir_hosts(
    self, details: bool = False, **kwargs: dict
) -> List[Union[str, Dict]]:
    """
    Retrieve a list of Nornir hosts managed by this worker.

    Args:
        details (bool): If True, returns detailed information about each host.
        **kwargs (dict): Hosts filters to apply when retrieving hosts.

    Returns:
        List[Dict]: A list of hosts with optional detailed information.
    """
    filters = {k: kwargs.pop(k) for k in list(kwargs.keys()) if k in FFun_functions}
    filtered_nornir = FFun(self.nr, **filters)
    if details:
        return Result(
            result={
                host_name: {
                    "platform": str(host.platform),
                    "hostname": str(host.hostname),
                    "port": str(host.port),
                    "groups": [str(g) for g in host.groups],
                    "username": str(host.username),
                }
                for host_name, host in filtered_nornir.inventory.hosts.items()
            }
        )
    else:
        return Result(result=list(filtered_nornir.inventory.hosts))

get_inventory(**kwargs: dict) -> Dict ¤

Retrieve running Nornir inventory for requested hosts

Parameters:

Name Type Description Default
**kwargs dict

Fx filters used to filter the inventory.

{}

Returns:

Name Type Description
Dict Dict

A dictionary representation of the filtered inventory.

Source code in norfab\workers\nornir_worker.py
702
703
704
705
706
707
708
709
710
711
712
713
714
def get_inventory(self, **kwargs: dict) -> Dict:
    """
    Retrieve running Nornir inventory for requested hosts

    Args:
        **kwargs (dict): Fx filters used to filter the inventory.

    Returns:
        Dict: A dictionary representation of the filtered inventory.
    """
    filters = {k: kwargs.pop(k) for k in list(kwargs.keys()) if k in FFun_functions}
    filtered_nornir = FFun(self.nr, **filters)
    return Result(result=filtered_nornir.inventory.dict(), task="get_inventory")

get_version() -> Dict ¤

Retrieve the versions of various libraries and system information.

This method collects the version information of a predefined set of libraries and system details such as the Python version and platform. It attempts to import each library and fetch its version. If a library is not found, it is skipped.

Returns:

Name Type Description
dict Dict

a dictionary with the library names as keys and their respective version numbers as values. If a library is not found, its value will be an empty string.

Source code in norfab\workers\nornir_worker.py
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
def get_version(self) -> Dict:
    """
    Retrieve the versions of various libraries and system information.

    This method collects the version information of a predefined set of libraries
    and system details such as the Python version and platform. It attempts to
    import each library and fetch its version. If a library is not found, it is
    skipped.

    Returns:
        dict: a dictionary with the library names as keys and their respective
            version numbers as values. If a library is not found, its value will be
            an empty string.
    """
    libs = {
        "norfab": "",
        "scrapli": "",
        "scrapli-netconf": "",
        "scrapli-community": "",
        "paramiko": "",
        "netmiko": "",
        "napalm": "",
        "nornir": "",
        "ncclient": "",
        "nornir-netmiko": "",
        "nornir-napalm": "",
        "nornir-scrapli": "",
        "nornir-utils": "",
        "tabulate": "",
        "xmltodict": "",
        "puresnmp": "",
        "pygnmi": "",
        "pyyaml": "",
        "jmespath": "",
        "jinja2": "",
        "ttp": "",
        "nornir-salt": "",
        "lxml": "",
        "ttp-templates": "",
        "ntc-templates": "",
        "cerberus": "",
        "pydantic": "",
        "requests": "",
        "textfsm": "",
        "N2G": "",
        "dnspython": "",
        "pythonping": "",
        "python": sys.version.split(" ")[0],
        "platform": sys.platform,
    }
    # get version of packages installed
    for pkg in libs.keys():
        try:
            libs[pkg] = importlib.metadata.version(pkg)
        except importlib.metadata.PackageNotFoundError:
            pass

    return Result(result=libs)

get_watchdog_stats() ¤

Retrieve the statistics from the watchdog.

Returns:

Name Type Description
Result

An object containing the statistics from the watchdog.

Source code in norfab\workers\nornir_worker.py
775
776
777
778
779
780
781
782
def get_watchdog_stats(self):
    """
    Retrieve the statistics from the watchdog.

    Returns:
        Result: An object containing the statistics from the watchdog.
    """
    return Result(result=self.watchdog.stats())

get_watchdog_configuration() ¤

Retrieves the current configuration of the watchdog.

Returns:

Name Type Description
Result

An object containing the watchdog configuration.

Source code in norfab\workers\nornir_worker.py
784
785
786
787
788
789
790
791
def get_watchdog_configuration(self):
    """
    Retrieves the current configuration of the watchdog.

    Returns:
        Result: An object containing the watchdog configuration.
    """
    return Result(result=self.watchdog.configuration())

get_watchdog_connections() ¤

Retrieve the list of connections curently managed by watchdog.

Returns:

Name Type Description
Result

An instance of the Result class containing the current watchdog connections.

Source code in norfab\workers\nornir_worker.py
793
794
795
796
797
798
799
800
801
def get_watchdog_connections(self):
    """
    Retrieve the list of connections curently managed by watchdog.

    Returns:
        Result: An instance of the Result class containing the current
            watchdog connections.
    """
    return Result(result=self.watchdog.connections_get())

task(plugin: str, **kwargs) -> Result ¤

Execute a Nornir task plugin.

This method dynamically imports and executes a specified Nornir task plugin, using the provided arguments and keyword arguments. The plugin attribute can refer to a file to fetch from a file service, which must contain a function named task that accepts a Nornir task object as the first positional argument.

Example of a custom task function file:

# define connection name for RetryRunner to properly detect it
CONNECTION_NAME = "netmiko"

# create task function
def task(nornir_task_object, **kwargs):
    pass
Note

The CONNECTION_NAME must be defined within the custom task function file if RetryRunner is in use. Otherwise, the connection retry logic is skipped, and connections to all hosts are initiated simultaneously up to the number of num_workers.

Parameters:

Name Type Description Default
plugin str

The path to the plugin function to import, or a NorFab URL to download a custom task.

required
**kwargs

Additional arguments to pass to the specified task plugin.

{}

Other Parameters:

Name Type Description
add_details bool

If True, adds task execution details to the results.

to_dict bool

If True, returns results as a dictionary. Defaults to True.

Filters

Any additional keyword arguments that match FFun_functions will be used as filters.

Returns:

Name Type Description
Result Result

An instance of the Result class containing the task execution results.

Raises:

Type Description
FileNotFoundError

If the specified plugin file cannot be downloaded.

Source code in norfab\workers\nornir_worker.py
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
def task(self, plugin: str, **kwargs) -> Result:
    """
    Execute a Nornir task plugin.

    This method dynamically imports and executes a specified Nornir task plugin,
    using the provided arguments and keyword arguments. The `plugin` attribute
    can refer to a file to fetch from a file service, which must contain a function
    named `task` that accepts a Nornir task object as the first positional argument.

    Example of a custom task function file:

    ```python
    # define connection name for RetryRunner to properly detect it
    CONNECTION_NAME = "netmiko"

    # create task function
    def task(nornir_task_object, **kwargs):
        pass
    ```

    Note:
        The `CONNECTION_NAME` must be defined within the custom task function file if
        RetryRunner is in use. Otherwise, the connection retry logic is skipped, and
        connections to all hosts are initiated simultaneously up to the number of `num_workers`.

    Args:
        plugin (str): The path to the plugin function to import, or a NorFab
            URL to download a custom task.
        **kwargs: Additional arguments to pass to the specified task plugin.

    Keyword Args:
        add_details (bool): If True, adds task execution details to the results.
        to_dict (bool): If True, returns results as a dictionary. Defaults to True.
        Filters: Any additional keyword arguments that match FFun_functions will be used as filters.

    Returns:
        Result: An instance of the Result class containing the task execution results.

    Raises:
        FileNotFoundError: If the specified plugin file cannot be downloaded.
    """
    # extract attributes
    add_details = kwargs.pop("add_details", False)  # ResultSerializer
    to_dict = kwargs.pop("to_dict", True)  # ResultSerializer
    filters = {k: kwargs.pop(k) for k in list(kwargs.keys()) if k in FFun_functions}
    ret = Result(task=f"{self.name}:task", result={} if to_dict else [])

    # download task from broker and load it
    if plugin.startswith("nf://"):
        function_text = self.fetch_file(plugin)
        if function_text is None:
            raise FileNotFoundError(
                f"{self.name} - '{plugin}' task plugin download failed"
            )

        # load task function running exec
        globals_dict = {}
        exec(function_text, globals_dict, globals_dict)
        task_function = globals_dict["task"]
    # import task function
    else:
        # below same as "from nornir.plugins.tasks import task_fun as task_function"
        task_fun = plugin.split(".")[-1]
        module = __import__(plugin, fromlist=[""])
        task_function = getattr(module, task_fun)

    self.nr.data.reset_failed_hosts()  # reset failed hosts
    filtered_nornir = FFun(self.nr, **filters)  # filter hosts

    # check if no hosts matched
    if not filtered_nornir.inventory.hosts:
        msg = (
            f"{self.name} - nothing to do, no hosts matched by filters '{filters}'"
        )
        log.debug(msg)
        ret.messages.append(msg)
        ret.status = "no_match"
        return ret

    nr = self._add_processors(filtered_nornir, kwargs)  # add processors

    # run task
    log.debug(f"{self.name} - running Nornir task '{plugin}', kwargs '{kwargs}'")
    with self.connections_lock:
        result = nr.run(task=task_function, **kwargs)

    ret.failed = result.failed  # failed is true if any of the hosts failed
    ret.result = ResultSerializer(result, to_dict=to_dict, add_details=add_details)

    self.watchdog.connections_clean()

    return ret

cli(commands: list = None, plugin: str = 'netmiko', dry_run: bool = False, run_ttp: str = None, job_data: str = None, to_dict: bool = True, add_details: bool = False, **kwargs) -> dict ¤

Task to collect show commands output from devices using Command Line Interface (CLI).

Parameters:

Name Type Description Default
commands list

List of commands to send to devices.

None
plugin str

Plugin name to use. Valid options are netmiko, scrapli, napalm.

'netmiko'
dry_run bool

If True, do not send commands to devices, just return them.

False
run_ttp str

TTP Template to run.

None
job_data str

URL to YAML file with data or dictionary/list of data to pass on to Jinja2 rendering context.

None
to_dict bool

If True, returns results as a dictionary.

True
add_details bool

If True, adds task execution details to the results.

False
**kwargs

Additional arguments to pass to the specified task plugin.

{}

Returns:

Name Type Description
dict dict

A dictionary with the results of the CLI task.

Raises:

Type Description
UnsupportedPluginError

If the specified plugin is not supported.

FileNotFoundError

If the specified TTP template or job data file cannot be downloaded.

Source code in norfab\workers\nornir_worker.py
 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
def cli(
    self,
    commands: list = None,
    plugin: str = "netmiko",
    dry_run: bool = False,
    run_ttp: str = None,
    job_data: str = None,
    to_dict: bool = True,
    add_details: bool = False,
    **kwargs,
) -> dict:
    """
    Task to collect show commands output from devices using Command Line Interface (CLI).

    Args:
        commands (list, optional): List of commands to send to devices.
        plugin (str, optional): Plugin name to use. Valid options are
            ``netmiko``, ``scrapli``, ``napalm``.
        dry_run (bool, optional): If True, do not send commands to devices,
            just return them.
        run_ttp (str, optional): TTP Template to run.
        job_data (str, optional): URL to YAML file with data or dictionary/list
            of data to pass on to Jinja2 rendering context.
        to_dict (bool, optional): If True, returns results as a dictionary.
        add_details (bool, optional): If True, adds task execution details
            to the results.
        **kwargs: Additional arguments to pass to the specified task plugin.

    Returns:
        dict: A dictionary with the results of the CLI task.

    Raises:
        UnsupportedPluginError: If the specified plugin is not supported.
        FileNotFoundError: If the specified TTP template or job data file
            cannot be downloaded.
    """
    job_data = job_data or {}
    filters = {k: kwargs.pop(k) for k in list(kwargs.keys()) if k in FFun_functions}
    timeout = self.current_job["timeout"] * 0.9
    ret = Result(task=f"{self.name}:cli", result={} if to_dict else [])

    # decide on what send commands task plugin to use
    if plugin == "netmiko":
        task_plugin = netmiko_send_commands
        if kwargs.get("use_ps"):
            kwargs.setdefault("timeout", timeout)
        else:
            kwargs.setdefault("read_timeout", timeout)
    elif plugin == "scrapli":
        task_plugin = scrapli_send_commands
        kwargs.setdefault("timeout_ops", timeout)
    elif plugin == "napalm":
        task_plugin = napalm_send_commands
    else:
        raise UnsupportedPluginError(f"Plugin '{plugin}' not supported")

    self.nr.data.reset_failed_hosts()  # reset failed hosts
    filtered_nornir = FFun(self.nr, **filters)  # filter hosts

    # check if no hosts matched
    if not filtered_nornir.inventory.hosts:
        msg = (
            f"{self.name} - nothing to do, no hosts matched by filters '{filters}'"
        )
        log.debug(msg)
        ret.messages.append(msg)
        ret.status = "no_match"
        return ret

    # download TTP template
    if self.is_url(run_ttp):
        downloaded = self.fetch_file(run_ttp)
        kwargs["run_ttp"] = downloaded
        if downloaded is None:
            msg = f"{self.name} - TTP template download failed '{run_ttp}'"
            raise FileNotFoundError(msg)
    # use TTP template as is - inline template or ttp://xyz path
    elif run_ttp:
        kwargs["run_ttp"] = run_ttp

    # download job data
    job_data = self.load_job_data(job_data)

    nr = self._add_processors(filtered_nornir, kwargs)  # add processors

    # render commands using Jinja2 on a per-host basis
    if commands:
        commands = commands if isinstance(commands, list) else [commands]
        for host in nr.inventory.hosts.values():
            rendered = self.jinja2_render_templates(
                templates=commands,
                context={
                    "host": host,
                    "norfab": self.client,
                    "nornir": self,
                    "job_data": job_data,
                },
                filters=self.add_jinja2_filters(),
            )
            host.data["__task__"] = {"commands": rendered}

    # run task
    log.debug(
        f"{self.name} - running cli commands '{commands}', kwargs '{kwargs}', is cli dry run - '{dry_run}'"
    )
    if dry_run is True:
        result = nr.run(
            task=nr_test, use_task_data="commands", name="dry_run", **kwargs
        )
    else:
        with self.connections_lock:
            result = nr.run(task=task_plugin, **kwargs)

    ret.failed = result.failed  # failed is true if any of the hosts failed
    ret.result = ResultSerializer(result, to_dict=to_dict, add_details=add_details)

    # remove __task__ data
    for host_name, host_object in nr.inventory.hosts.items():
        _ = host_object.data.pop("__task__", None)

    self.watchdog.connections_update(nr, plugin)
    self.watchdog.connections_clean()

    return ret

cfg(config: list, plugin: str = 'netmiko', dry_run: bool = False, to_dict: bool = True, add_details: bool = False, job_data: str = None, **kwargs) -> dict ¤

Task to send configuration commands to devices using Command Line Interface (CLI).

Parameters:

Name Type Description Default
config list

List of commands to send to devices.

required
plugin str

Plugin name to use. Valid options are:

  • netmiko - use Netmiko to configure devices
  • scrapli - use Scrapli to configure devices
  • napalm - use NAPALM to configure devices
'netmiko'
dry_run bool

If True, will not send commands to devices but just return them.

False
to_dict bool

If True, returns results as a dictionary. Defaults to True.

True
add_details bool

If True, adds task execution details to the results.

False
job_data str

URL to YAML file with data or dictionary/list of data to pass on to Jinja2 rendering context.

None
**kwargs

Additional arguments to pass to the task plugin.

{}

Returns:

Name Type Description
dict dict

A dictionary with the results of the configuration task.

Raises:

Type Description
UnsupportedPluginError

If the specified plugin is not supported.

FileNotFoundError

If the specified job data file cannot be downloaded.

Source code in norfab\workers\nornir_worker.py
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
1116
1117
1118
1119
1120
1121
1122
def cfg(
    self,
    config: list,
    plugin: str = "netmiko",
    dry_run: bool = False,
    to_dict: bool = True,
    add_details: bool = False,
    job_data: str = None,
    **kwargs,
) -> dict:
    """
    Task to send configuration commands to devices using Command Line Interface (CLI).

    Args:
        config (list): List of commands to send to devices.
        plugin (str, optional): Plugin name to use. Valid options are:

            - netmiko - use Netmiko to configure devices
            - scrapli - use Scrapli to configure devices
            - napalm - use NAPALM to configure devices

        dry_run (bool, optional): If True, will not send commands to devices but just return them.
        to_dict (bool, optional): If True, returns results as a dictionary. Defaults to True.
        add_details (bool, optional): If True, adds task execution details to the results.
        job_data (str, optional): URL to YAML file with data or dictionary/list of data to pass on to Jinja2 rendering context.
        **kwargs: Additional arguments to pass to the task plugin.

    Returns:
        dict: A dictionary with the results of the configuration task.

    Raises:
        UnsupportedPluginError: If the specified plugin is not supported.
        FileNotFoundError: If the specified job data file cannot be downloaded.
    """
    config = config if isinstance(config, list) else [config]
    filters = {k: kwargs.pop(k) for k in list(kwargs.keys()) if k in FFun_functions}
    ret = Result(task=f"{self.name}:cfg", result={} if to_dict else [])

    # decide on what send commands task plugin to use
    if plugin == "netmiko":
        task_plugin = netmiko_send_config
    elif plugin == "scrapli":
        task_plugin = scrapli_send_config
    elif plugin == "napalm":
        task_plugin = napalm_configure
    else:
        raise UnsupportedPluginError(f"Plugin '{plugin}' not supported")

    self.nr.data.reset_failed_hosts()  # reset failed hosts
    filtered_nornir = FFun(self.nr, **filters)  # filter hosts

    # check if no hosts matched
    if not filtered_nornir.inventory.hosts:
        msg = (
            f"{self.name} - nothing to do, no hosts matched by filters '{filters}'"
        )
        log.debug(msg)
        ret.messages.append(msg)
        ret.status = "no_match"
        return ret

    job_data = self.load_job_data(job_data)

    nr = self._add_processors(filtered_nornir, kwargs)  # add processors

    # render config using Jinja2 on a per-host basis
    for host in nr.inventory.hosts.values():
        rendered = self.jinja2_render_templates(
            templates=config,
            context={
                "host": host,
                "norfab": self.client,
                "nornir": self,
                "job_data": job_data,
            },
            filters=self.add_jinja2_filters(),
        )
        host.data["__task__"] = {"config": rendered}

    # run task
    log.debug(
        f"{self.name} - sending config commands '{config}', kwargs '{kwargs}', is dry_run - '{dry_run}'"
    )
    if dry_run is True:
        result = nr.run(
            task=nr_test, use_task_data="config", name="dry_run", **kwargs
        )
    else:
        with self.connections_lock:
            result = nr.run(task=task_plugin, **kwargs)

    ret.failed = result.failed  # failed is true if any of the hosts failed
    ret.result = ResultSerializer(result, to_dict=to_dict, add_details=add_details)

    # remove __task__ data
    for host_name, host_object in nr.inventory.hosts.items():
        _ = host_object.data.pop("__task__", None)

    self.watchdog.connections_update(nr, plugin)
    self.watchdog.connections_clean()

    return ret

test(suite: Union[list, str], subset: str = None, dry_run: bool = False, remove_tasks: bool = True, failed_only: bool = False, return_tests_suite: bool = False, job_data: str = None, **kwargs) -> dict ¤

Function to test networks using a suite of tests.

Parameters:

Name Type Description Default
suite Union[list, str]

Path to YAML file with tests or a list of test definitions.

required
subset str

List or string with comma-separated non-case-sensitive glob patterns to filter tests by name. Ignored if dry_run is True.

None
dry_run bool

If True, returns produced per-host tests suite content only.

False
remove_tasks bool

If False, results will include other tasks output.

True
failed_only bool

If True, returns test results for failed tests only.

False
return_tests_suite bool

If True, returns rendered per-host tests suite content in addition to test results using a dictionary with results and suite keys.

False
job_data str

URL to YAML file with data or dictionary/list of data to pass on to Jinja2 rendering context.

None
**kwargs

Any additional arguments to pass on to the Nornir service task.

{}

Returns:

Name Type Description
dict dict

A dictionary containing the test results. If return_tests_suite is True, the dictionary will contain both the test results and the rendered test suite.

Note

Result failed attribute is set to True if any of the tests failed for any of the hosts.

Raises:

Type Description
RuntimeError

If there is an error in rendering the Jinja2 templates or loading the YAML.

Source code in norfab\workers\nornir_worker.py
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
def test(
    self,
    suite: Union[list, str],
    subset: str = None,
    dry_run: bool = False,
    remove_tasks: bool = True,
    failed_only: bool = False,
    return_tests_suite: bool = False,
    job_data: str = None,
    **kwargs,
) -> dict:
    """
    Function to test networks using a suite of tests.

    Args:
        suite (Union[list, str]): Path to YAML file with tests or a list of test definitions.
        subset (str, optional): List or string with comma-separated non-case-sensitive glob
            patterns to filter tests by name. Ignored if dry_run is True.
        dry_run (bool, optional): If True, returns produced per-host tests suite content only.
        remove_tasks (bool, optional): If False, results will include other tasks output.
        failed_only (bool, optional): If True, returns test results for failed tests only.
        return_tests_suite (bool, optional): If True, returns rendered per-host tests suite
            content in addition to test results using a dictionary with ``results`` and ``suite`` keys.
        job_data (str, optional): URL to YAML file with data or dictionary/list of data
            to pass on to Jinja2 rendering context.
        **kwargs: Any additional arguments to pass on to the Nornir service task.

    Returns:
        dict: A dictionary containing the test results. If `return_tests_suite` is True,
            the dictionary will contain both the test results and the rendered test suite.

    Note:
        Result `failed` attribute is set to True if any of the tests failed for any of the hosts.

    Raises:
        RuntimeError: If there is an error in rendering the Jinja2 templates or loading the YAML.
    """
    tests = {}  # dictionary to hold per-host test suites
    add_details = kwargs.get("add_details", False)  # ResultSerializer
    to_dict = kwargs.get("to_dict", True)  # ResultSerializer
    filters = {k: kwargs.pop(k) for k in list(kwargs.keys()) if k in FFun_functions}
    ret = Result(task=f"{self.name}:test", result={} if to_dict else [])
    suites = {}  # dictionary to hold combined test suites

    self.nr.data.reset_failed_hosts()  # reset failed hosts
    filtered_nornir = FFun(self.nr, **filters)  # filter hosts

    # check if no hosts matched
    if not filtered_nornir.inventory.hosts:
        msg = (
            f"{self.name} - nothing to do, no hosts matched by filters '{filters}'"
        )
        log.debug(msg)
        ret.messages.append(msg)
        ret.status = "no_match"
        if return_tests_suite is True:
            ret.result = {"test_results": [], "suite": {}}
        return ret

    # download job data
    job_data = self.load_job_data(job_data)

    # generate per-host test suites
    for host_name, host in filtered_nornir.inventory.hosts.items():
        # render suite using Jinja2
        try:
            rendered_suite = self.jinja2_render_templates(
                templates=[suite],
                context={
                    "host": host,
                    "norfab": self.client,
                    "nornir": self,
                    "job_data": job_data,
                },
                filters=self.add_jinja2_filters(),
            )
        except Exception as e:
            msg = f"{self.name} - '{suite}' Jinja2 rendering failed: '{e}'"
            raise RuntimeError(msg)
        # load suit using YAML
        try:
            tests[host_name] = yaml.safe_load(rendered_suite)
        except Exception as e:
            msg = f"{self.name} - '{suite}' YAML load failed: '{e}'"
            raise RuntimeError(msg)

    # validate tests suite
    try:
        _ = modelTestsProcessorSuite(tests=tests)
    except Exception as e:
        msg = f"{self.name} - '{suite}' suite validation failed: '{e}'"
        raise RuntimeError(msg)

    # download pattern, schema and custom function files
    for host_name in tests.keys():
        for index, item in enumerate(tests[host_name]):
            for k in ["pattern", "schema", "function_file"]:
                if self.is_url(item.get(k)):
                    item[k] = self.fetch_file(
                        item[k], raise_on_fail=True, read=True
                    )
                    if k == "function_file":
                        item["function_text"] = item.pop(k)
            tests[host_name][index] = item

    # save per-host tests suite content before mutating it
    if return_tests_suite is True:
        return_suite = copy.deepcopy(tests)

    log.debug(f"{self.name} - running test '{suite}', is dry run - '{dry_run}'")
    # run dry run task
    if dry_run is True:
        result = filtered_nornir.run(
            task=nr_test, name="tests_dry_run", ret_data_per_host=tests
        )
        ret.result = ResultSerializer(
            result, to_dict=to_dict, add_details=add_details
        )
    # combine per-host tests in suites based on task and arguments
    # Why - to run tests using any nornir service tasks with various arguments
    else:
        for host_name, host_tests in tests.items():
            for test in host_tests:
                dhash = hashlib.md5()
                test_args = test.pop("norfab", {})
                nrtask = test_args.get("nrtask", "cli")
                assert nrtask in [
                    "cli",
                    "network",
                    "cfg",
                    "task",
                ], f"{self.name} - unsupported NorFab Nornir Service task '{nrtask}'"
                test_json = json.dumps(test_args, sort_keys=True).encode()
                dhash.update(test_json)
                test_hash = dhash.hexdigest()
                suites.setdefault(test_hash, {"params": test_args, "tests": {}})
                suites[test_hash]["tests"].setdefault(host_name, [])
                suites[test_hash]["tests"][host_name].append(test)
        log.debug(
            f"{self.name} - combined per-host tests suites based on NorFab Nornir Service task and arguments:\n{suites}"
        )
        # run test suites collecting output from devices
        for tests_suite in suites.values():
            nrtask = tests_suite["params"].pop("nrtask", "cli")
            function_kwargs = {
                **tests_suite["params"],
                **kwargs,
                **filters,
                "tests": tests_suite["tests"],
                "remove_tasks": remove_tasks,
                "failed_only": failed_only,
                "subset": subset,
            }
            result = getattr(self, nrtask)(
                **function_kwargs
            )  # returns Result object
            # save test results into overall results
            if to_dict == True:
                for host_name, host_res in result.result.items():
                    ret.result.setdefault(host_name, {})
                    ret.result[host_name].update(host_res)
                    # set return result failed to true if any of the tests failed
                    for test_res in host_res.values():
                        if add_details:
                            if test_res["result"] != "PASS":
                                ret.failed = True
                        elif test_res != "PASS":
                            ret.failed = True
            else:
                ret.result.extend(result.result)
                # set return result failed to true if any of the tests failed
                if any(r["result"] != "PASS" for r in result.result):
                    ret.failed = True

    # check if need to return tests suite content
    if return_tests_suite is True:
        ret.result = {"test_results": ret.result, "suite": return_suite}

    return ret

network(fun, **kwargs) -> dict ¤

Task to call various network-related utility functions.

Parameters:

Name Type Description Default
fun str

The name of the utility function to call.

required
kwargs dict

Arguments to pass to the utility function.

{}

Available utility functions:

  • resolve_dns Resolves hosts' hostname DNS, returning IP addresses using nornir_salt.plugins.tasks.network.resolve_dns Nornir-Salt function.
  • ping Executes ICMP ping to host using nornir_salt.plugins.tasks.network.ping Nornir-Salt function.

Returns:

Name Type Description
dict dict

A dictionary containing the results of the network utility function.

Raises:

Type Description
UnsupportedPluginError

If the specified utility function is not supported.

Source code in norfab\workers\nornir_worker.py
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
def network(self, fun, **kwargs) -> dict:
    """
    Task to call various network-related utility functions.

    Args:
        fun (str): The name of the utility function to call.
        kwargs (dict): Arguments to pass to the utility function.

    Available utility functions:

    - **resolve_dns** Resolves hosts' hostname DNS, returning IP addresses using
        `nornir_salt.plugins.tasks.network.resolve_dns` Nornir-Salt function.
    - **ping** Executes ICMP ping to host using `nornir_salt.plugins.tasks.network.ping`
        Nornir-Salt function.

    Returns:
        dict: A dictionary containing the results of the network utility function.

    Raises:
        UnsupportedPluginError: If the specified utility function is not supported.
    """
    kwargs["call"] = fun
    return self.task(
        plugin="nornir_salt.plugins.tasks.network",
        **kwargs,
    )

parse(plugin: str = 'napalm', getters: str = 'get_facts', template: str = None, commands: list = None, to_dict: bool = True, add_details: bool = False, **kwargs) ¤

Parse network device output using specified plugin and options.

Parameters:

Name Type Description Default
plugin str

The plugin to use for parsing. Options are:

  • napalm - parse devices output using NAPALM getters
  • ttp - use TTP Templates to parse devices output
  • textfsm - use TextFSM templates to parse devices output
'napalm'
getters str

The getters to use with the "napalm" plugin.

'get_facts'
template str

The template to use with the "ttp" or "textfsm" plugin.

None
commands list

The list of commands to run with the "ttp" or "textfsm" plugin.

None
to_dict bool

Whether to convert the result to a dictionary.

True
add_details bool

Whether to add details to the result.

False
**kwargs

Additional keyword arguments to pass to the plugin.

{}

Returns:

Name Type Description
Result

A Result object containing the parsed data.

Raises:

Type Description
UnsupportedPluginError

If the specified plugin is not supported.

Source code in norfab\workers\nornir_worker.py
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
def parse(
    self,
    plugin: str = "napalm",
    getters: str = "get_facts",
    template: str = None,
    commands: list = None,
    to_dict: bool = True,
    add_details: bool = False,
    **kwargs,
):
    """
    Parse network device output using specified plugin and options.

    Args:
        plugin (str): The plugin to use for parsing. Options are:

            - napalm - parse devices output using NAPALM getters
            - ttp - use TTP Templates to parse devices output
            - textfsm - use TextFSM templates to parse devices output

        getters (str): The getters to use with the "napalm" plugin.
        template (str): The template to use with the "ttp" or "textfsm" plugin.
        commands (list): The list of commands to run with the "ttp" or "textfsm" plugin.
        to_dict (bool): Whether to convert the result to a dictionary.
        add_details (bool): Whether to add details to the result.
        **kwargs: Additional keyword arguments to pass to the plugin.

    Returns:
        Result: A Result object containing the parsed data.

    Raises:
        UnsupportedPluginError: If the specified plugin is not supported.
    """
    filters = {k: kwargs.pop(k) for k in list(kwargs.keys()) if k in FFun_functions}
    ret = Result(task=f"{self.name}:parse", result={} if to_dict else [])

    self.nr.data.reset_failed_hosts()  # reset failed hosts
    filtered_nornir = FFun(self.nr, **filters)  # filter hosts

    # check if no hosts matched
    if not filtered_nornir.inventory.hosts:
        msg = (
            f"{self.name} - nothing to do, no hosts matched by filters '{filters}'"
        )
        log.debug(msg)
        ret.messages.append(msg)
        ret.status = "no_match"
        return ret

    if plugin == "napalm":
        nr = self._add_processors(filtered_nornir, kwargs)  # add processors
        result = nr.run(task=napalm_get, getters=getters, **kwargs)
        ret.result = ResultSerializer(
            result, to_dict=to_dict, add_details=add_details
        )
        ret.failed = result.failed  # failed is true if any of the hosts failed
    elif plugin == "ttp":
        result = self.cli(
            commands=commands or [],
            run_ttp=template,
            **filters,
            **kwargs,
            to_dict=to_dict,
            add_details=add_details,
            plugin="netmiko",
        )
        ret.result = result.result
    elif plugin == "textfsm":
        result = self.cli(
            commands=commands,
            **filters,
            **kwargs,
            to_dict=to_dict,
            add_details=add_details,
            use_textfsm=True,
            textfsm_template=template,
            plugin="netmiko",
        )
        ret.result = result.result
    else:
        raise UnsupportedPluginError(f"Plugin '{plugin}' not supported")

    return ret

file_copy(source_file: str, plugin: str = 'netmiko', to_dict: bool = True, add_details: bool = False, dry_run: bool = False, **kwargs) -> Dict ¤

Task to transfer files to and from hosts using SCP.

Parameters:

Name Type Description Default
source_file str

The path or URL of the source file to be copied in nf://path/to/file format

required
plugin str

The plugin to use for file transfer. Supported plugins:

  • netmiko - uses netmiko_file_transfer task plugin.
'netmiko'
to_dict bool

Whether to return the result as a dictionary. Defaults to True.

True
add_details bool

Whether to add detailed information to the result. Defaults to False.

False
dry_run bool

If True, performs a dry run without making any changes. Defaults to False.

False
**kwargs

Additional arguments to pass to the file transfer plugin.

{}

Returns:

Name Type Description
dict Dict

The result of the file copy operation.

Raises:

Type Description
UnsupportedPluginError

If the specified plugin is not supported.

Source code in norfab\workers\nornir_worker.py
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
def file_copy(
    self,
    source_file: str,
    plugin: str = "netmiko",
    to_dict: bool = True,
    add_details: bool = False,
    dry_run: bool = False,
    **kwargs,
) -> Dict:
    """
    Task to transfer files to and from hosts using SCP.

    Args:
        source_file (str): The path or URL of the source file to be copied in
            ``nf://path/to/file`` format
        plugin (str, optional): The plugin to use for file transfer. Supported plugins:

            - netmiko - uses `netmiko_file_transfer` task plugin.

        to_dict (bool, optional): Whether to return the result as a dictionary. Defaults to True.
        add_details (bool, optional): Whether to add detailed information to the result. Defaults to False.
        dry_run (bool, optional): If True, performs a dry run without making any changes. Defaults to False.
        **kwargs: Additional arguments to pass to the file transfer plugin.

    Returns:
        dict: The result of the file copy operation.

    Raises:
        UnsupportedPluginError: If the specified plugin is not supported.
    """
    filters = {k: kwargs.pop(k) for k in list(kwargs.keys()) if k in FFun_functions}
    timeout = self.current_job["timeout"] * 0.9
    ret = Result(task=f"{self.name}:file_copy", result={} if to_dict else [])

    # download file from broker
    if self.is_url(source_file):
        source_file_local = self.fetch_file(
            source_file, raise_on_fail=True, read=False
        )

    # decide on what send commands task plugin to use
    if plugin == "netmiko":
        task_plugin = netmiko_file_transfer
        kwargs["source_file"] = source_file_local
        kwargs.setdefault("socket_timeout", timeout / 5)
        kwargs.setdefault("dest_file", os.path.split(source_file_local)[-1])
    else:
        raise UnsupportedPluginError(f"Plugin '{plugin}' not supported")

    self.nr.data.reset_failed_hosts()  # reset failed hosts
    filtered_nornir = FFun(self.nr, **filters)  # filter hosts

    # check if no hosts matched
    if not filtered_nornir.inventory.hosts:
        msg = (
            f"{self.name} - nothing to do, no hosts matched by filters '{filters}'"
        )
        log.debug(msg)
        ret.messages.append(msg)
        ret.status = "no_match"
        return ret

    nr = self._add_processors(filtered_nornir, kwargs)  # add processors

    # run task
    log.debug(
        f"{self.name} - running file copy with arguments '{kwargs}', is dry run - '{dry_run}'"
    )
    if dry_run is True:
        result = nr.run(task=nr_test, name="file_copy_dry_run", **kwargs)
    else:
        with self.connections_lock:
            result = nr.run(task=task_plugin, **kwargs)

    ret.failed = result.failed  # failed is true if any of the hosts failed
    ret.result = ResultSerializer(result, to_dict=to_dict, add_details=add_details)

    self.watchdog.connections_update(nr, plugin)
    self.watchdog.connections_clean()

    return ret

runtime_inventory(action, **kwargs) -> dict ¤

Task to work with Nornir runtime (in-memory) inventory.

Supported actions:

  • create_host or create - creates new host or replaces existing host object
  • read_host or read - read host inventory content
  • update_host or update - non recursively update host attributes if host exists in Nornir inventory, do not create host if it does not exist
  • delete_host or delete - deletes host object from Nornir Inventory
  • load - to simplify calling multiple functions
  • read_inventory - read inventory content for groups, default and hosts
  • read_host_data - to return host's data under provided path keys
  • list_hosts - return a list of inventory's host names
  • list_hosts_platforms - return a dictionary of hosts' platforms
  • update_defaults - non recursively update defaults attributes

Parameters:

Name Type Description Default
action

action to perform on inventory

required
kwargs

argument to use with the calling action

{}
Source code in norfab\workers\nornir_worker.py
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
def runtime_inventory(self, action, **kwargs) -> dict:
    """
    Task to work with Nornir runtime (in-memory) inventory.

    Supported actions:

    - `create_host` or `create` - creates new host or replaces existing host object
    - `read_host` or `read` - read host inventory content
    - `update_host` or `update` - non recursively update host attributes if host exists
        in Nornir inventory, do not create host if it does not exist
    - `delete_host` or `delete` - deletes host object from Nornir Inventory
    - `load` - to simplify calling multiple functions
    - `read_inventory` - read inventory content for groups, default and hosts
    - `read_host_data` - to return host's data under provided path keys
    - `list_hosts` - return a list of inventory's host names
    - `list_hosts_platforms` - return a dictionary of hosts' platforms
    - `update_defaults` - non recursively update defaults attributes

    Args:
        action: action to perform on inventory
        kwargs: argument to use with the calling action
    """
    # clean up kwargs
    _ = kwargs.pop("progress", None)
    self.event(f"Performing '{action}' action")
    return Result(result=InventoryFun(self.nr, call=action, **kwargs))

nb_get_next_ip(*args, **kwargs) ¤

Task to query next available IP address from Netbox service

Source code in norfab\workers\nornir_worker.py
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
def nb_get_next_ip(self, *args, **kwargs):
    """Task to query next available IP address from Netbox service"""
    reply = self.client.run_job(
        "netbox",
        "get_next_ip",
        args=args,
        kwargs=kwargs,
        workers="any",
        timeout=30,
    )
    # reply is a dict of {worker_name: results_dict}
    result = list(reply.values())[0]

    return result["result"]