Skip to main content

Artifact management plugin

regenerate_host_artifact

Regenerates a host artifact for a given task.

This function regenerates the named artifact for the host's InfrahubNode through the Infrahub SDK.

Parameters

ParameterTypeRequiredDefaultDescription
taskTaskYesThe task instance containing host-related data.
artifactstrYesThe name of the artifact to regenerate.

Returns

Result — An object representing the outcome of the operation, indicating success or failure.

Raises

ExceptionDescription
ExceptionPropagates any error raised by the Infrahub SDK if the artifact cannot be generated.

Examples

Regenerate artifact for a given device

from nornir import InitNornir
from nornir.core.plugins.inventory import InventoryPluginRegister
from nornir_infrahub.plugins.inventory.infrahub import InfrahubInventory
from nornir_infrahub.plugins.tasks import regenerate_host_artifact

from nornir_utils.plugins.functions import print_result


def main():
InventoryPluginRegister.register("InfrahubInventory", InfrahubInventory)
nr = InitNornir(inventory=...)

eos_devices = nr.filter(platform="eos")

# regenerate an artifact for a host
print_result(eos_devices.run(task=regenerate_host_artifact, artifact="startup-config"))

return 0


if __name__ == "__main__":
raise SystemExit(main())

generate_artifacts

Generates an artifact for a given task.

This function retrieves the artifact definition from the InfrahubNode and triggers its generation through the Infrahub SDK.

Parameters

ParameterTypeRequiredDefaultDescription
taskTaskYesThe task instance containing host-related data.
artifactstrYesThe name of the artifact to generate.
timeoutintNo10Retained for compatibility; ignored by the SDK path. Defaults to 10.

Returns

Result — An object representing the outcome of the operation, indicating success or failure.

Raises

ExceptionDescription
ExceptionPropagates any error raised by the Infrahub SDK if the artifact cannot be generated.

Examples

Example generating artifacts

from nornir import InitNornir
from nornir.core.plugins.inventory import InventoryPluginRegister
from nornir_infrahub.plugins.inventory.infrahub import InfrahubInventory
from nornir_infrahub.plugins.tasks import generate_artifacts


def main():
InventoryPluginRegister.register("InfrahubInventory", InfrahubInventory)
nr = InitNornir(inventory=...)

# generate_artifacts, generates the artifact for all the targets in the Artifact definition
# we only need to run this task once, per artifact definition
run_once = nr.filter(name="jfk1-edge1")
result = run_once.run(task=generate_artifacts, artifact="startup-config", timeout=20)
ocfg_result = run_once.run(task=generate_artifacts, artifact="openconfig-interfaces", timeout=20)

return 0


if __name__ == "__main__":
raise SystemExit(main())

get_artifact

Retrieves the specified artifact from the Infrahub storage.

This function fetches an artifact node associated with the given artifact name or id and sends a request to retrieve its stored content. The response is returned as JSON or text, depending on the artifact's content type.

Parameters

ParameterTypeRequiredDefaultDescription
taskTaskYesThe task instance containing host-related data.
artifactstrNoThe name of the artifact to retrieve.
artifact_idstrNoThe id of the artifact to retrieve.

Returns

Result — An object containing the retrieved artifact data, its content type, and the success status of the operation.

Raises

ExceptionDescription
RuntimeErrorIf neither or both of artifact and artifact_id are provided.
ExceptionPropagates any error raised by the Infrahub SDK if the artifact cannot be retrieved.

Examples

Example getting artifacts from Infrahub

from nornir import InitNornir
from nornir.core.plugins.inventory import InventoryPluginRegister
from nornir_infrahub.plugins.inventory.infrahub import InfrahubInventory
from nornir_infrahub.plugins.tasks import get_artifact
from nornir_utils.plugins.functions import print_result


def main():
InventoryPluginRegister.register("InfrahubInventory", InfrahubInventory)
nr = InitNornir(inventory=...)

eos_devices = nr.filter(platform="eos")
# retrieves the artifact for all the hosts in the inventory
result = eos_devices.run(task=get_artifact, artifact="startup-config")
print_result(result)

return 0


if __name__ == "__main__":
raise SystemExit(main())