diff --git a/nornir/__init__.py b/nornir/__init__.py index a7cf4728..a0b6286a 100644 --- a/nornir/__init__.py +++ b/nornir/__init__.py @@ -1,7 +1,7 @@ import pkg_resources -from nornir.init_nornir import InitNornir +from nornir.init_nornir import InitNornir, InitNornirAsync __version__ = pkg_resources.get_distribution("nornir").version -__all__ = ("InitNornir", "__version__") +__all__ = ("InitNornir", "InitNornirAsync", "__version__") diff --git a/nornir/init_nornir.py b/nornir/init_nornir.py index 7de8f8a4..2e1faab9 100644 --- a/nornir/init_nornir.py +++ b/nornir/init_nornir.py @@ -12,6 +12,22 @@ from nornir.core.state import GlobalState +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_async() + + 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 {})) + + def load_inventory( config: Config, ) -> Inventory: @@ -74,3 +90,40 @@ def InitNornir( config=config, data=data, ) + + +def InitNornirAsync( + 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 + """ + ConnectionPluginRegister.auto_register() + + if config_file: + config = Config.from_file(config_file, **kwargs) + else: + config = Config.from_dict(**kwargs) + + data = GlobalState(dry_run=dry_run) + + config.logging.configure() + + return Nornir( + inventory=load_inventory_async(config), + runner=load_runner(config), + config=config, + data=data, + )