Skip to content

Commit

Permalink
Modify howto to use InitNornir & fix group -> groups (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
ogenstad authored and dbarrosop committed Jul 12, 2018
1 parent 631cbbe commit 3f03436
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions docs/howto/writing_a_custom_inventory.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Writing a custom inventory
==========================

If you have your own backend with host information or you don't like the provided ones you can write your own custom inventory. Doing so is quite easy. A continuation you can find a very simple one with static data::
If you have your own backend with host information or you don't like the provided ones you can write your own custom inventory. Doing so is quite easy. A continuation you can find a very simple one with static data.

.. code-block:: python
from builtins import super
Expand All @@ -15,13 +17,13 @@ If you have your own backend with host information or you don't like the provide
hosts = {
"host1": {
"data1": "value1",
"data2": "value2".
"group": "my_group1",
"data2": "value2",
"groups": ["my_group1"],
},
"host2": {
"data1": "value1",
"data2": "value2".
"group": "my_group1",
"data2": "value2",
"groups": ["my_group1"],
}
}
groups = {
Expand All @@ -30,19 +32,27 @@ If you have your own backend with host information or you don't like the provide
"more_data2": "more_value2",
}
}
defaults = {
"location": "internet",
"language": "Python",
}
# passing the data to the parent class so the data is
# transformed into actual Host/Group objects
super().__init__(hosts, groups, **kwargs)
# and set default data for all hosts
super().__init__(hosts, groups, defaults, **kwargs)
So if you want to make it dynamic everything you have to do is get the data yourself and organize it in a similar format to the one described in the example above.

.. note:: it is not mandatory to use groups. Feel free to skip the attribute ``group`` and just pass and empty dict or ``None`` to ``super()``.
.. note:: it is not mandatory to use groups or defaults. Feel free to skip the attribute ``groups`` and just pass and empty dict or ``None`` to ``super()``.

Finally, to have nornir use it, you can:

.. code-block:: python
Finally, to have nornir use it, you can do::
from nornir.core import InitNornir
nr = InitNornir(inventory=MyInventory)
inv = MyInventory()
nornir = Nornir(inventory=inv)
And that's it, you now have your own inventory plugin :)

0 comments on commit 3f03436

Please sign in to comment.