Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to support Async inventory #882

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions nornir/init_nornir.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -39,7 +60,7 @@ def load_runner(
return runner


def InitNornir(
async def InitNornirAsync(
config_file: str = "",
dry_run: bool = False,
**kwargs: Any,
Expand Down Expand Up @@ -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)
)
4 changes: 4 additions & 0 deletions nornir/plugins/inventory/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down