One of the many advantages of using nornir is that it will be parallelize the execution of tasks for you. The way it works is as follows:
- You trigger the parallelization by running a task via
num_workers > 1(defaults to
num_workers == 1we run the task over all hosts one after the other in a simple loop. This is useful for troubleshooting/debugging, for writing to disk/database or just for printing on screen.
- When parallelizing tasks nornir will use a different thread for each host.
Below you can see a simple diagram illustrating how this works:
Note that you can create tasks with other tasks inside. When tasks are nested the inner tasks will run serially for that host in parallel to other hosts. This is useful as it let’s you control the flow of the execution at your own will. For instance, you could compose a different workflow to the previous one as follows:
Why would you do this? Most of the time you will want to group as many tasks as possible. That will ensure your script runs as fast as possible. However, some tasks might require to be run after ensuring the some others are done. For instance, you could do something like:
- Configure everything in parallel
- Run some verification tests
- Enable services