With 3.7.0b1, your type checker and your editor know the type of the result of a task, and you can run one action on hosts of different vendors with an Operation. You can also follow the progress of a run, cancel it and set time limits, and applications that run an event loop can run async def tasks and load the inventory on that loop. All these features are optional. Existing scripts keep working, and ThreadedRunner is still the default runner.
Note
3.7.0b1 is a pre-release. Install it with pip install "nornir==3.7.0b1". pip install --upgrade nornir does not install it. The new APIs can still change before 3.7.0, so do not use it in production.
Testing the 3.7 pre-releases explains how to install it, what to test, and where to send feedback.
Important
Nornir 3.7 is the last release that supports Python 3.10. 3.7.0b1 supports Python 3.10 to 3.15.
New features
- Read typed results. Give a task a return annotation, such as
-> Result[Facts]or-> str.nr.run()then returnsAggregatedResult[Facts], andmulti.resulthas the typeFacts. The type checker reports a wrong field name before the code runs, and the editor completes the field names.Task.run(),stream(),submit()and the async methods work the same way. A task annotated with a bareResult, a task without a return annotation (with mypy and ty) and a generator task keepAny. Nothing changes at runtime. Tutorial (#1129) - Run one action across vendors with Operations. Define an action once as an
Operation, with an input model and an output model. Implement it with one plain task for each platform and connection, registered withOperationRegistry.registeror with entry points.nr.run_operation(GetConfig, params)then runs, on each host, the task registered for the platform of the host, without a condition onhost.platformin your code. The result of each host holds an instance of the output model, which the type checker knows. A host of a platform without a registered task fails on its own, and the other hosts continue. How-to guide (#1089, #1115) - Follow, cancel and time-limit runs. Follow each host with
nr.stream(), and read only the final result of each host withresults(). Start a run in the background withnr.submit(), and stop it withhandle.cancel(). Set a time limit for each host withper_host_timeout. This works with synchronous tasks onThreadedRunnerandSerialRunner(streamonly), and withAsyncioRunner. How-to guide (#1086, #1113, #1115) - Run
async deftasks. Select theasynciorunner and callawait nr.arun(...)to run tasks on the event loop of your application. Connection plugins can now support sync, asyncio or both. How-to guide (#1085, #1111) - Load the inventory from an async application.
nr = await AInitNornir(...)takes the same arguments asInitNornirand does not block the event loop. Existing inventory plugins work with it. How-to guide (#1087, #1112)
To test the async features against real systems, use the pre-releases of two plugins: nornir-scrapli==1.6.0a1 for network devices and nornir-infrahub==1.3.0a1 for the inventory. They are also examples for plugin developers. See Plugins that use the new APIs.
Behaviour changes when the code runs
Nornir.runandTask.runnow raiseAsyncTaskOnSyncRunErrorfor anasync deftask. Before, the task did not run, but the host was reported as successful (#1111).- A task that contains
yieldnow runs. Before, the task did not run, but the host was reported as successful (#1113). InitNornirnow raisesTransformFunctionNotSyncErrorfor a transform function defined withasync def. Before, the transform function did not run (#1112).Host.close_connectionsnow tries to close every connection, then raises the first error. Before, it stopped at the first connection that it could not close (#1111).
Behaviour changes for type checkers
A type checker can report errors in code that passed with 3.6.1. Nothing changes when the code runs.
- A variable that holds the runs of tasks with different return types now needs the annotation
AggregatedResult, for exampleresult: AggregatedResult = nr.run(task=backup)(#1129). - The result of a failed host, which holds the error text, is typed as if the task succeeded. Check
multi.failedbefore you readmulti.result(#1129). Result,AggregatedResult,MultiResultand the run objects are now generic. A bareResultstill meansResult[Any]with a type checker that supports type variable defaults (PEP 696). An older type checker in strict mode reports a bareResultas missing its type parameter. mypy 2.x is recommended (#1115, #1129).
Send feedback
Run your scripts, your plugins and your type checker with 3.7.0b1, and tell us what works and what does not. Comment on the issue of the feature (#1085 for the asyncio runner, #1086 for runs, #1087 for the async inventory, #1089 for Operations, #1129 for typed results), or open a new issue for a bug. You can also write in the #nornir channel of the OpsMill Discord server or in the networktocode Slack team.
The changelog has the complete list of changes.
If you tested 3.7.0a1: typed results, Operations and results() on a stream are new in 3.7.0b1.
Full Changelog: v3.6.1...v3.7.0b1