Transforming Inventory Data¶

[1]:

%cat transforming_inventory_data/inventory/hosts.yaml

---
rtr00:
data:
user: automation_user
rtr01:
data:
user: automation_user


And we want to do two things:

1. Map user to username

You can easily do that using a transform_function. For instance:

Modifying hosts’ data¶

You can modify inventory data (regardless of the plugin you are using) on the fly easily by passing a transform_function like this:

[2]:

from nornir import InitNornir
import pprint

# This function receives a Host object for manipulation

nr = InitNornir(
inventory={
"plugin": "nornir.plugins.inventory.simple.SimpleInventory",
"options": {
"host_file": "transforming_inventory_data/inventory/hosts.yaml",
"group_file": "transforming_inventory_data/inventory/groups.yaml",
},
},
)
for name, host in nr.inventory.hosts.items():

rtr00.username: automation_user


Specifying the transform function in the configuration¶

You can also specify the transform function in the configuration. In order for that to work the function must be importable. For instance:

[3]:

%cat transforming_inventory_data/helpers.py

def adapt_host_data(host):
# This function receives a Host object for manipulation


Now let’s verify we can, indeed, import the function:

[4]:

from transforming_inventory_data.helpers import adapt_host_data

[4]:

<function transforming_inventory_data.helpers.adapt_host_data(host)>


Now the only thing left to do is put the import path as the transform_function configuration option:

[5]:

%cat transforming_inventory_data/config.yaml

---
inventory:
plugin: nornir.plugins.inventory.simple.SimpleInventory
options:
host_file: "transforming_inventory_data/inventory/hosts.yaml"
group_file: "transforming_inventory_data/inventory/groups.yaml"


And then we won’t need to specify it when calling InitNornir:

[6]:

from nornir import InitNornir
import pprint

nr = InitNornir(
config_file="transforming_inventory_data/config.yaml",
)
for name, host in nr.inventory.hosts.items():

rtr00.username: automation_user


You might be in a situation where you may want to prompt the user for some information, for instance, a password. You can leverage the defaults for something like this (although you could just put in under the hosts themselves or the groups). Let’s see an example:

[7]:

from nornir import InitNornir

# let's pretend we used raw_input or something like that

nr = InitNornir(
config_file="transforming_inventory_data/config.yaml",
)

Before setting password:  None


For more information on how inheritance works check the tutorial section inheritance model

Passing options to the transform function¶

You can also pass options to the transform function, like in this example:

[8]:

from nornir import InitNornir

options = {
"something_else": "unused"
}

nr = InitNornir(
inventory={
"plugin": "nornir.plugins.inventory.simple.SimpleInventory",
"options": {
"host_file": "transforming_inventory_data/inventory/hosts.yaml",
"group_file": "transforming_inventory_data/inventory/groups.yaml",
},
"transform_function_options": options,
}
)
for name, host in nr.inventory.hosts.items():

rtr00.password: None


Using ConnectionOptions¶

Additionally, you might come across the need to include certain ConnectionOptions to be able to connect to your devices.
Documentation specific to nornir can be found here and for napalm can be found here.

The following example shows how to correctly do so in the helpers.py:

[9]:

from nornir.core.inventory import ConnectionOptions

# This function receives a Host object for manipulation
host.connection_options["napalm"] = ConnectionOptions(
extras={
"optional_args": {
}
}
)


You can also specify them as a default for your entire inventory in this manner:

[10]:

from nornir.core.inventory import ConnectionOptions

nr.inventory.defaults.connection_options = ConnectionOptions(extras={"optional_args":{"secret":"my_secret"}})