files
Collection of task plugins to work with files saved by TofileProcessor
.
Files Plugin Sample usage
Code to invoke files
task plugins:
from nornir import InitNornir
from nornir_salt.plugins.processors.ToFileProcessor import ToFileProcessor
from nornir_salt.plugins.tasks import file_read, file_remove, file_list, file_diff, files
from nornir_netmiko import netmiko_send_command
nr = InitNornir(config_file="config.yaml")
# save results to files
nr_with_tf = nr.with_processors(
[ToFileProcessor(tf="config_for_read", base_url="./tofile_outputs/")]
)
nr_with_tf.run(
task=netmiko_send_command,
command_string="show run"
)
# retrieve saved files content on demand
res = nr.run(
task=file_read,
filegroup="config_for_read",
base_url="./tofile_outputs/",
)
Files Plugin API reference
file_read
- nornir_salt.plugins.tasks.files.file_read(task, filegroup, base_url: str = '/var/nornir-salt/', task_name: str = None, last: int = 1, index: str = 'common')
Function to read text files content saved by
ToFileProcessor
.This task reconstructs previously saved results and returns Nornir Result objects.
- Parameters
filegroup – (str or list)
tf
group or list oftf
file group names of files to loadbase_url – (str) OS path to folder with saved files, default “/var/nornir-salt/”
last – (int) what file to read, default is 1 - the most current one
task_name – (str) name of task to read previous results for, returns all results if
task_name
is empty.index – (str)
ToFileProcessor
index filename to read files information from
- Returns
Result object with file read content
file_list
- nornir_salt.plugins.tasks.files.file_list(task, filegroup=None, base_url: str = '/var/nornir-salt/', index: str = 'common')
Function to produce a list of text files saved by
ToFileProcessor
- Parameters
filegroup – (str or list)
tf
group or list oftf
file group names of files to listbase_url – (str) OS path to folder where to save files, default “/var/nornir-salt/”
index – (str)
ToFileProcessor
index filename to read files information from
- Returns
Nornir Result object with files list
file_remove
- nornir_salt.plugins.tasks.files.file_remove(task, filegroup, base_url: str = '/var/nornir-salt/', index: str = 'common', tf_index_lock: multiprocessing.context.BaseContext.Lock = None)
Function to remove files saved by
ToFileProcessor
- Parameters
filegroup – (str or list or bool)
tf
or list oftf
file group names of files to remove. If set to True will remove all filesbase_url – (str) OS path to folder with saved files, default “/var/nornir-salt/”
index – (str)
ToFileProcessor
index filename to read files information fromtf_index_lock – Lock object to safely access and update files index to make remove operation thread and/or multiprocessing safe
- Returns
Result object with removed files summary
file_diff
- nornir_salt.plugins.tasks.files.file_diff(task, filegroup, base_url: str = '/var/nornir-salt/', task_name: str = None, last=None, index: str = 'common')
Function to read text files content saved by
ToFileProcessor
and return difference.- Parameters
filegroup – (str or list)
tf
file group name of files to use for diff, if list, runs difference for each filegroupbase_url – (str) OS path to folder with saved files, default “/var/nornir-salt/”
last – (int or list or str) files to diff, default is - [1, 2] - last 1 and last 2
task_name – (str) name of task to read previous results for, diffs all results if
task_name
is empty.index – (str)
ToFileProcessor
index filename to read files information from
- Returns
Result object with files difference, if files are identical result is True
files dispatcher function
- nornir_salt.plugins.tasks.files.files(task, call, *args, **kwargs)
Dispatcher function to call one of the functions.
- Parameters
call – (str) nickname of function to call
arg – (list) function arguments
kwargs – (dict) function key-word arguments
- Returns
function execution results
Call function nicknames:
ls
orlist
- calls file_listrm
orremove
ordelete
- calls file_removeread
- calls file_readdiff
- calls file_diff