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 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)

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

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:

{
    "config": {
        "IOL1": [
            {
                "filename": "./tofile_outputs/config__22_August_2021_14_08_33__IOL1.txt",
                "tasks": {
                    "show run | inc ntp": {
                        "span": [0, 48],
                        "content_type": "str"
                }
            }
        ]
    }
}

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.