load_credentials
load_credentials
is an transform_function to add credentials to every host. The transform_function_options username
and password
are supportet as well as the environment variables NORNIR_USERNAME
and NORNIR_PASSWORD
. transform_function_options have priority.
Example
Loading inventory without load_credentials
[1]:
from nornir import InitNornir
nr = InitNornir(
inventory={
"plugin": "SimpleInventory",
"options": {"host_file": "data/hosts.yaml", "group_file": "data/groups.yaml"}
}
)
for host in nr.inventory.hosts.values():
print(f"{host.name}\tUser:{host.username}\tPassword:{host.password}")
dev1.group_1 User:a_user Password:a_password
dev2.group_1 User:None Password:from_group1
dev3.group_2 User:None Password:None
dev4.group_2 User:None Password:from_parent_group
dev5.no_group User:None Password:None
Using transform_funtion_options
Set the password for all hosts using the parameter password
[2]:
from nornir import InitNornir
nr = InitNornir(
inventory={
"plugin": "SimpleInventory",
"options": {"host_file": "data/hosts.yaml", "group_file": "data/groups.yaml"},
"transform_function": "load_credentials",
"transform_function_options": {"password": "TopSecretP4ss"}
}
)
for host in nr.inventory.hosts.values():
print(f"{host.name}\tUser:{host.username}\tPassword:{host.password}")
dev1.group_1 User:a_user Password:TopSecretP4ss
dev2.group_1 User:None Password:TopSecretP4ss
dev3.group_2 User:None Password:TopSecretP4ss
dev4.group_2 User:None Password:TopSecretP4ss
dev5.no_group User:None Password:TopSecretP4ss
Setting username and password
[3]:
from nornir import InitNornir
nr = InitNornir(
inventory={
"plugin": "SimpleInventory",
"options": {"host_file": "data/hosts.yaml", "group_file": "data/groups.yaml"},
"transform_function": "load_credentials",
"transform_function_options": {"username": "myUser", "password": "TopSecretP4ss"}
}
)
for host in nr.inventory.hosts.values():
print(f"{host.name}\tUser:{host.username}\tPassword:{host.password}")
dev1.group_1 User:myUser Password:TopSecretP4ss
dev2.group_1 User:myUser Password:TopSecretP4ss
dev3.group_2 User:myUser Password:TopSecretP4ss
dev4.group_2 User:myUser Password:TopSecretP4ss
dev5.no_group User:myUser Password:TopSecretP4ss
Using environment variables
Set the variable NORNIR_PASSWORD
[4]:
%set_env NORNIR_PASSWORD=AnotherPassWord!
env: NORNIR_PASSWORD=AnotherPassWord!
[5]:
from nornir import InitNornir
nr = InitNornir(
inventory={
"plugin": "SimpleInventory",
"options": {"host_file": "data/hosts.yaml", "group_file": "data/groups.yaml"},
"transform_function": "load_credentials"
}
)
for host in nr.inventory.hosts.values():
print(f"{host.name}\tUser:{host.username}\tPassword:{host.password}")
dev1.group_1 User:a_user Password:AnotherPassWord!
dev2.group_1 User:None Password:AnotherPassWord!
dev3.group_2 User:None Password:AnotherPassWord!
dev4.group_2 User:None Password:AnotherPassWord!
dev5.no_group User:None Password:AnotherPassWord!
Also set the environment variable NORNIR_USERNAME
[6]:
%set_env NORNIR_USERNAME=AnotherUser
env: NORNIR_USERNAME=AnotherUser
[7]:
from nornir import InitNornir
nr = InitNornir(
inventory={
"plugin": "SimpleInventory",
"options": {"host_file": "data/hosts.yaml", "group_file": "data/groups.yaml"},
"transform_function": "load_credentials"
}
)
for host in nr.inventory.hosts.values():
print(f"{host.name}\tUser:{host.username}\tPassword:{host.password}")
dev1.group_1 User:AnotherUser Password:AnotherPassWord!
dev2.group_1 User:AnotherUser Password:AnotherPassWord!
dev3.group_2 User:AnotherUser Password:AnotherPassWord!
dev4.group_2 User:AnotherUser Password:AnotherPassWord!
dev5.no_group User:AnotherUser Password:AnotherPassWord!
The transform_function_option can overwrite the environment variables
[8]:
from nornir import InitNornir
nr = InitNornir(
inventory={
"plugin": "SimpleInventory",
"options": {"host_file": "data/hosts.yaml", "group_file": "data/groups.yaml"},
"transform_function": "load_credentials",
"transform_function_options": {"password": "TopSecretP4ss"}
}
)
for host in nr.inventory.hosts.values():
print(f"{host.name}\tUser:{host.username}\tPassword:{host.password}")
dev1.group_1 User:AnotherUser Password:TopSecretP4ss
dev2.group_1 User:AnotherUser Password:TopSecretP4ss
dev3.group_2 User:AnotherUser Password:TopSecretP4ss
dev4.group_2 User:AnotherUser Password:TopSecretP4ss
dev5.no_group User:AnotherUser Password:TopSecretP4ss