Get commands from Nornir result

Hi everyone,

I would like to know if the variable collecting the results of a Nornir run stores the commands previously sent by a Nornir run.

Here is an example of code to explain the problem I have:

################################################

[…]

def nornir_group_of_tasks(task, list_of_commands):

for cmd in list_of_commands:
    task.run(task=netmiko_send_command,command_string=cmd)

def main():

# Device parameters
h = {'my_device': {'hostname': '192.168.0.1','port': 22,'username': 'bla','bla': 'linux','platform': 'linux'}}
g = {}
d = {}

# Initialization of the Nornir object
nr = InitNornir(inventory={"plugin": "nornir.plugins.inventory.simple.SimpleInventory","options": {"hosts": h, "groups": g, "defaults": d}})

# Command to send to the device
list_of_commands = ["uname -a","pwd"]

# Runnning the commands with Nornir
result = nr.run(task=nornir_group_of_tasks, list_of_commands=list_of_commands)

# Display the results
for device in result:

    # Display device name
    device_name = str(result[device][0].host)
    print(device_name)

    for command in result[device]:

        # Display command
        command_name = str(command.name)
        print(command_name)

################################################

Output would give:

###############################################

my_device
nornir_group_of_tasks
netmiko_send_command
netmiko_send_command

###############################################

But I would like to have that:

###############################################

my_device
uname -a
pwd

###############################################

Inside “result” we can get the name of the device and the result for each command sent (in the above example those are the results of 2 commands). However I wonder if it is possible to get the values of the “list_of_commands” list (that is “uname -a” and “pwd”).

I am not totally sure but I think it was possible to get those values in a previous version of Nornir but I cannot get that with nornir 2.4.0.

So I can see 2 options for me: either the variable “result” stores the commands and then I can use it or I use “list_of_commands” list but that would be my second choice since there is no direct link between those variables.

So are the commands used in a Nornir run stored inside the “result” variable? If yes how to get them?

Hi
netmiko_send_command has an name attribute.

for cmd in list_of_commands:
    task.run(task=netmiko_send_command,command_string=cmd, name=cmd)

should return exactly what you want

Andreas

This is exactly what I was looking for. Now I get the command inside the returned object.

Thank you Andreas.