ToFileProcessor Plugin

Processor plugin to save task execution results to file.

ToFileProcessor Sample Usage

Code to demonstrate how to use ToFileProcessor plugin:

from nornir import InitNornir
from nornir_netmiko import netmiko_send_command
from nornir_salt.plugins.processors import ToFileProcessor

nr = InitNornir(config_file="config.yaml")

nr_with_processor = nr.with_processors([
    ToFileProcessor(tf="config", base_url="./Output/")
])

nr_with_processor.run(
    task=netmiko_send_command,
    command_string="show run"
)

ToFileProcessor reference

nornir_salt.plugins.processors.ToFileProcessor.ToFileProcessor(tf, base_url='/var/nornir-salt/', max_files=5, index=None, skip_failed=False, tf_index_lock=None)

ToFileProcessor can save task execution results to file on a per host basis. If multiple tasks present, results of all of them saved in same file.

Parameters
  • tf – (str) name of the file groups content

  • base_url – (str) OS path to folder where to save files, default “/var/nornir-salt/”

  • max_files – (int) default is 5, maximum number of file for given tf file group

  • index – (str) index filename to read and store files data into

  • skip_failed – (bool) if True, do not save failed task results, default is False

  • tf_index_lock – Lock object to safely access and update files index to make files saving operation thread and/or multiprocessing safe

Files saved under base_url location, where individual filename formed using string:

{tf}__{timestamp}__{rand}__{hostname}.txt

Where:

  • tf - value of tf attribute

  • timestamp - %d_%B_%Y_%H_%M_%S time formatted string, e.g. “12_June_2021_21_48_11”

  • rand - random integer in range from 10 to 1000

  • hostname - name attribute of host

In addition, tf_index_{index}.json file created under base_url to track files created using dictionary structure:

{
    "facts": {
        "ceos1": [
            {
                "filename": "/var/salt-nornir/nrp1/files/facts__10_June_2022_13_42_36__683__ceos1.txt",
                "tasks": {
                    "run_ttp": {
                        "content_type": "json",
                        "span": [0, 139]
                    }
                },
                "timestamp": "10 Jun 2022 13:42:36 UTC"
            }
        ]
    },
    "show_clock_output": {
        "ceos1": [
            {
                "filename": "/var/salt-nornir/nrp1/files/show_clock_output__10_June_2022_14_34_20__1__ceos1.txt",
                "tasks": {
                    "show clock": {
                        "content_type": "str",
                        "span": [0, 59]
                    }
                },
                "timestamp": "10 Jun 2022 14:34:20 UTC"
            },
        ]
    }
}

Where config is tf attribute value, "show run | inc ntp": [0, 48] - show run | inc ntp task results span indexes inside ./tofile_outputs/config__22_August_2021_14_08_33__IOL1.txt text file.

tf_index_{index}.json used by other plugins to retrieve previous results for the task, it could be considered as a simplified index database.