From 395d0025e7f1d4747b018bfd086d7e096c67e660 Mon Sep 17 00:00:00 2001 From: Damien Garros Date: Wed, 29 Nov 2023 12:27:29 +0100 Subject: [PATCH 1/2] Leverage Async for InitNornir and inventory_load --- nornir/init_nornir.py | 54 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/nornir/init_nornir.py b/nornir/init_nornir.py index 7de8f8a4..99d15d09 100644 --- a/nornir/init_nornir.py +++ b/nornir/init_nornir.py @@ -1,5 +1,7 @@ from typing import Any +import asyncio + from nornir.core import Nornir from nornir.core.configuration import Config from nornir.core.inventory import Inventory @@ -14,18 +16,37 @@ def load_inventory( config: Config, +) -> Inventory: + return asyncio.run(load_inventory_async(config=config)) + + +async def load_inventory_async( + config: Config, ) -> Inventory: InventoryPluginRegister.auto_register() inventory_plugin = InventoryPluginRegister.get_plugin(config.inventory.plugin) - inv = inventory_plugin(**config.inventory.options).load() + inventory = inventory_plugin(**config.inventory.options) + if asyncio.iscoroutinefunction(inventory.load): + inv = await inventory.load() + else: + inv = inventory.load() if config.inventory.transform_function: TransformFunctionRegister.auto_register() transform_function = TransformFunctionRegister.get_plugin( config.inventory.transform_function ) - for h in inv.hosts.values(): - transform_function(h, **(config.inventory.transform_function_options or {})) + + if asyncio.iscoroutinefunction(transform_function): + for h in inv.hosts.values(): + await transform_function( + h, **(config.inventory.transform_function_options or {}) + ) + else: + for h in inv.hosts.values(): + transform_function( + h, **(config.inventory.transform_function_options or {}) + ) return inv @@ -39,7 +60,7 @@ def load_runner( return runner -def InitNornir( +async def InitNornirAsync( config_file: str = "", dry_run: bool = False, **kwargs: Any, @@ -69,8 +90,31 @@ def InitNornir( config.logging.configure() return Nornir( - inventory=load_inventory(config), + inventory=await load_inventory_async(config), runner=load_runner(config), config=config, data=data, ) + + +def InitNornir( + config_file: str = "", + dry_run: bool = False, + **kwargs: Any, +) -> Nornir: + """ + Arguments: + config_file(str): Path to the configuration file (optional) + dry_run(bool): Whether to simulate changes or not + configure_logging: Whether to configure logging or not. This argument is being + deprecated. Please use logging.enabled parameter in the configuration + instead. + **kwargs: Extra information to pass to the + :obj:`nornir.core.configuration.Config` object + + Returns: + :obj:`nornir.core.Nornir`: fully instantiated and configured + """ + return asyncio.run( + InitNornirAsync(config_file=config_file, dry_run=dry_run, **kwargs) + ) From 2993c21588c6fe59fbb04fec1241dbc1f67ee1af Mon Sep 17 00:00:00 2001 From: Damien Garros Date: Thu, 30 Nov 2023 21:16:40 +0100 Subject: [PATCH 2/2] Add simple Async inventory --- nornir/plugins/inventory/simple.py | 4 ++++ pyproject.toml | 1 + 2 files changed, 5 insertions(+) diff --git a/nornir/plugins/inventory/simple.py b/nornir/plugins/inventory/simple.py index 0ee0884c..493225d6 100644 --- a/nornir/plugins/inventory/simple.py +++ b/nornir/plugins/inventory/simple.py @@ -123,3 +123,7 @@ def load(self) -> Inventory: h.groups = ParentGroups([groups[g] for g in h.groups]) return Inventory(hosts=hosts, groups=groups, defaults=defaults) + +class SimpleInventoryAsync(SimpleInventory): + async def load(self) -> Inventory: + return super().load() \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 1ffc46fd..4114da9f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,6 +8,7 @@ build-backend = "poetry.masonry.api" [tool.poetry.plugins."nornir.plugins.inventory"] "SimpleInventory" = "nornir.plugins.inventory.simple:SimpleInventory" +"SimpleInventoryAsync" = "nornir.plugins.inventory.simple:SimpleInventoryAsync" [tool.poetry] name = "nornir"