pyats_send_commands
This task plugin uses PyATS devices execute
method
to send multiple commands to devices pre-processing commands accordingly.
Pre-processing includes:
Check and if any - retrieve per-host commands from host’s inventory data
task.host.data["__task__"]["commands"]
or fromtask.host.data["__task__"]["filename"]
If command is a multi-line string, split it to individual lines or form a list with single command
Iterate over commands list and remove empty strings
Iterate over commands and replace
new_line_char
with\n
new line
There are several modes that pyats_send_commands
task plugin can operate in:
If
parse
is true and device platform supports it, send commands one by one parsing their output waiting forinterval
in between commands ifinterval
provided, if no parser available for this platform to parse command output, exception message returned for such a commandIf
interval
argument provided, commands send one by one to device usingexecute
method sleeping for giveninterval
between commandsIf
via
argument refers to connections pool object, send commands in parallel, commands execution order not guaranteedBy default, all commands supplied to device’s connection
execute
method as is
Dependencies:
PyATS library required
Genie library required
Useful links:
Sample Usage
Given this inventory:
host-1:
connection_options:
pyats:
extras:
devices:
host-1:
os: iosxe
credentials:
default:
username: nornir
password: nornir
connections:
default:
protocol: ssh
ip: 10.0.1.4
port: 22
vty_1:
protocol: ssh
ip: 10.0.1.4
pool: 3
Code to invoke pyats_send_commands
task:
from nornir_salt.plugins.tasks import pyats_send_commands
# send via "default" connection
output_via_default = nr.run(
task=pyats_send_commands,
commands=["show run", "show clock"]
)
# send via vty_1 connection pool of 3 SSH connections
output_via_pool = nr.run(
task=pyats_send_commands,
commands=["show run", "show clock"],
via="vty_1"
)
# send via "default" connection with 5s interval between commands
output_with_interval = nr.run(
task=pyats_send_commands,
commands=["show run", "show clock"],
interval=5
)
# send commands and parse output
output_parse = nr.run(
task=pyats_send_commands,
commands=["show hostname", "show clock"],
parse=True
)
Returns Nornir results object with individual tasks names set equal to commands sent to device.
Reference
- nornir_salt.plugins.tasks.pyats_send_commands.pyats_send_commands(task: nornir.core.task.Task, commands: list = None, interval: int = None, new_line_char: str = '_br_', via: str = 'default', parse: bool = False, split_lines: bool = True, **kwargs)
Nornir Task function to send show commands to devices using PyATS Unicon module.
Per-host
commands
can be provided using host’s objectdata
attribute with__task__
key with value set to dictionary withcommands
key containing a list of or a multiline string of commands to send to device, e.g.:print(host.data["__task__"]["commands"]) ["ping 1.1.1.1 source 1.1.1.2", "show clock"]
Alternatively,
__task__
can containfilename
key with commands string to send to device.- Parameters
kwargs – (dict) used with connection’s
execute
or testbed’sparse
method as**kwargs
commands – (list or str) list or multiline string of commands to send to device
interval – (int) interval between sending commands, default is None - commands send simultaneously
new_line_char – (str) characters to replace in commands with new line
\n
before sending command to device, default is_br_
, useful to simulate enter keyvia – (str) testbed inventory connection name, default is
default
parse – (bool) if True, parses command output and returns structured data
split_lines – (bool) if True split multiline string to commands, send multiline string to device as is otherwise
- Return result
Nornir result object with task results named after commands