-
Just learning Nornir after using Netmiko for some time so apologies if this is a bit basic but all the examples I see are running one task against a set of devices and displaying the output or running a task to update a set of devices. What is the recommended way of combining the two and using the output of the first to determine what to do in the second task eg. check for a specific acl and if it is on the device add a new IP to it (example is a bit contrived but it gives you the idea). I am assuming if you ran this as two completely separate tasks then that would mean two different threads and two different connections to the same device and if so is this actually a bad thing ie. is it inefficient having to make separate connections to the same device within the same script ? Is this where sub tasks come in and if so is there a trade off between simplicity of just making multiple connections vs efficiency of using multiple tasks within the same connection ? Appreciate it is a bit of an open ended question but any tips/suggestions would be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You can create a task to execute multiple tasks together, it should look similar to that If both tasks are using Netmiko it will reuse the same connection automatically def do_something_per_device(task: Task, *args, **kwargs) -> Result:
result1 = task.run(task=netmiko_send_command, command_string="my command", *args, **kwargs)
result2 = task.run(task=second_task, *args, **kwargs) |
Beta Was this translation helpful? Give feedback.
-
Nornir will generally keep the connection open (unless you explicitly close it). Here I am assuming the same connection plugin (i.e. Netmiko). So if you had two tasks, it would imply two different threads, but only one connection. That being said I would likely do something similar to what @dgarros referenced above and group the actions into a single task. I would do this as it would make the logic easier (and the program simpler). |
Beta Was this translation helpful? Give feedback.
You can create a task to execute multiple tasks together, it should look similar to that
If both tasks are using Netmiko it will reuse the same connection automatically