Nornit InitNornr initialization without hosts.yaml (dynamically)

Hi guys.
I wish to dynamically add hosts to the nornir from csv file.
I managed to do that by this way:

  1. InitNornir with a hosts.yaml, that contains only one fake host.
  2. Add hosts from csv to nornir, attach them to the appropriate group.
  3. Filter by this group and implement something.

How to avoid the first step? How to initialize nornir without the hosts.yaml (somehow indicate them in InitNornir function probably).

Here is my script:

nr = InitNornir(
inventory={            
    "plugin": "nornir.plugins.inventory.simple.SimpleInventory"},
core={"num_workers":10},
dry_run=False,         
logging={'enabled':False})

#print(nr.inventory.hosts)
for dev in csv_file:
nr.inventory.add_host(name=dev['ip'], hostname=dev['ip'], groups=['my_group']) #groups.yaml contains the my_group's settings

Thank you

I don’t have much experience with Nornir, but my take on this - you have at least 2 options here:

  1. Write up an inventory plugin to feed Nornir with hosts and groups from a csv file ( https://nornir.readthedocs.io/en/stable/howto/writing_a_custom_inventory_plugin.html )
  2. Generate a simple-inventory compatible yaml file from a csv, before initializing Nornir

I would personally go with option 2. You can for instance generate a hosts.yaml file, having groups.yaml pre-populated with known hostnames - and update groups.yaml either manually or automatically. It really depends on your workflow though.

1 Like

Thank you for the advice.
I followed the first way - just created a custom inventory with clear “hosts” dict and necessary groups options. That what I needed.