Skip to content

Commit

Permalink
Some poc inventory code
Browse files Browse the repository at this point in the history
  • Loading branch information
ktbyers committed Dec 1, 2023
1 parent 33c587a commit d6a763f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
4 changes: 2 additions & 2 deletions nornir/__init__.py
Original file line number Diff line number Diff line change
@@ -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__")
53 changes: 53 additions & 0 deletions nornir/init_nornir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
)

0 comments on commit d6a763f

Please sign in to comment.