inventory: YAMLInventory
YAMLInventory is an inventory plugin that builds the inventory from a few YAML files.
Data
Let’s see how it works by example. First let’s look at the different files the plugin takes:
[1]:
%cat data/hosts.yaml
---
dev1.group_1:
port: 65020
hostname: localhost
username: a_user
password: a_password
platform: eos
data:
my_var: comes_from_dev1.group_1
www_server: nginx
role: www
nested_data:
a_dict:
a: 1
b: 2
a_list: [1, 2]
a_string: asdasd
groups:
- group_1
connection_options:
paramiko:
port: 65020
hostname:
username: root
password: docker
platform: linux
extras: {}
dummy:
hostname: dummy_from_host
port:
username:
password:
platform:
extras:
blah: from_host
dev2.group_1:
port: 65021
hostname: localhost
platform: junos
data:
role: db
nested_data:
a_dict:
b: 2
c: 3
a_list: [2, 3]
a_string: qwe
groups:
- group_1
connection_options:
paramiko:
username: root
password: docker
platform: linux
extras: {}
dummy2:
username: dummy2_from_host
dev3.group_2:
port: 65022
hostname: localhost
platform: linux
data:
www_server: apache
role: www
groups:
- group_2
connection_options:
napalm:
platform: mock
dev4.group_2:
port: 65023
hostname: localhost
platform: linux
data:
my_var: comes_from_dev4.group_2
role: db
groups:
- group_2
- parent_group
connection_options:
paramiko:
hostname: localhost
username: root
password: docker
platform: linux
netmiko:
hostname: localhost
username: root
password: docker
platform: linux
dev5.no_group:
port: 65024
hostname: localhost
platform: linux
[2]:
%cat data/groups.yaml
---
parent_group:
password: from_parent_group
data:
a_var: blah
a_false_var: false
connection_options:
dummy:
hostname: dummy_from_parent_group
extras:
blah: from_group
dummy2:
hostname: dummy2_from_parent_group
extras:
blah: from_group
group_1:
password: from_group1
platform:
data:
my_var: comes_from_group_1
site: site1
groups:
- parent_group
group_2:
data:
site: site2
group_3:
data:
site: site2
[3]:
%cat data/defaults.yaml
---
username: root
password: docker
platform: linux
data:
my_var: comes_from_defaults
only_default: only_defined_in_default
connection_options:
dummy:
hostname: dummy_from_defaults
extras:
blah: from_defaults
Example
Once you have the yaml files, using the plugin is very simple, you just need to specify the plugin you want to use is YAMLInventory
and where to find the files:
[4]:
from nornir import InitNornir
nr = InitNornir(
inventory={
"plugin":"YAMLInventory",
"options": {
"host_file": "data/hosts.yaml",
"group_file": "data/groups.yaml",
"defaults_file": "data/defaults.yaml",
}
}
)
Just like that, you can verify the inventory playing with or just printing it as a dict()
[5]:
nr.inventory.dict()
[5]:
{'hosts': {'dev1.group_1': {'name': 'dev1.group_1',
'connection_options': {'paramiko': {'extras': {},
'hostname': None,
'port': 65020,
'username': 'root',
'password': 'docker',
'platform': 'linux'},
'dummy': {'extras': {'blah': 'from_host'},
'hostname': 'dummy_from_host',
'port': None,
'username': None,
'password': None,
'platform': None}},
'groups': ['group_1'],
'data': {'my_var': 'comes_from_dev1.group_1',
'www_server': 'nginx',
'role': 'www',
'nested_data': {'a_dict': {'a': 1, 'b': 2},
'a_list': [1, 2],
'a_string': 'asdasd'}},
'hostname': 'localhost',
'port': 65020,
'username': 'a_user',
'password': 'a_password',
'platform': 'eos'},
'dev2.group_1': {'name': 'dev2.group_1',
'connection_options': {'paramiko': {'extras': {},
'hostname': None,
'port': None,
'username': 'root',
'password': 'docker',
'platform': 'linux'},
'dummy2': {'extras': None,
'hostname': None,
'port': None,
'username': 'dummy2_from_host',
'password': None,
'platform': None}},
'groups': ['group_1'],
'data': {'role': 'db',
'nested_data': {'a_dict': {'b': 2, 'c': 3},
'a_list': [2, 3],
'a_string': 'qwe'}},
'hostname': 'localhost',
'port': 65021,
'username': None,
'password': None,
'platform': 'junos'},
'dev3.group_2': {'name': 'dev3.group_2',
'connection_options': {'napalm': {'extras': None,
'hostname': None,
'port': None,
'username': None,
'password': None,
'platform': 'mock'}},
'groups': ['group_2'],
'data': {'www_server': 'apache', 'role': 'www'},
'hostname': 'localhost',
'port': 65022,
'username': None,
'password': None,
'platform': 'linux'},
'dev4.group_2': {'name': 'dev4.group_2',
'connection_options': {'paramiko': {'extras': None,
'hostname': 'localhost',
'port': None,
'username': 'root',
'password': 'docker',
'platform': 'linux'},
'netmiko': {'extras': None,
'hostname': 'localhost',
'port': None,
'username': 'root',
'password': 'docker',
'platform': 'linux'}},
'groups': ['group_2', 'parent_group'],
'data': {'my_var': 'comes_from_dev4.group_2', 'role': 'db'},
'hostname': 'localhost',
'port': 65023,
'username': None,
'password': None,
'platform': 'linux'},
'dev5.no_group': {'name': 'dev5.no_group',
'connection_options': {},
'groups': [],
'data': {},
'hostname': 'localhost',
'port': 65024,
'username': None,
'password': None,
'platform': 'linux'}},
'groups': {'parent_group': {'name': 'parent_group',
'connection_options': {'dummy': {'extras': {'blah': 'from_group'},
'hostname': 'dummy_from_parent_group',
'port': None,
'username': None,
'password': None,
'platform': None},
'dummy2': {'extras': {'blah': 'from_group'},
'hostname': 'dummy2_from_parent_group',
'port': None,
'username': None,
'password': None,
'platform': None}},
'groups': [],
'data': {'a_var': 'blah', 'a_false_var': False},
'hostname': None,
'port': None,
'username': None,
'password': 'from_parent_group',
'platform': None},
'group_1': {'name': 'group_1',
'connection_options': {},
'groups': ['parent_group'],
'data': {'my_var': 'comes_from_group_1', 'site': 'site1'},
'hostname': None,
'port': None,
'username': None,
'password': 'from_group1',
'platform': None},
'group_2': {'name': 'group_2',
'connection_options': {},
'groups': [],
'data': {'site': 'site2'},
'hostname': None,
'port': None,
'username': None,
'password': None,
'platform': None},
'group_3': {'name': 'group_3',
'connection_options': {},
'groups': [],
'data': {'site': 'site2'},
'hostname': None,
'port': None,
'username': None,
'password': None,
'platform': None}},
'defaults': {'data': {'my_var': 'comes_from_defaults',
'only_default': 'only_defined_in_default'},
'connection_options': {'dummy': {'extras': {'blah': 'from_defaults'},
'hostname': 'dummy_from_defaults',
'port': None,
'username': None,
'password': None,
'platform': None}},
'hostname': None,
'port': None,
'username': 'root',
'password': 'docker',
'platform': 'linux'}}