Sidarion / netbox-joined-inventory

Netbox_joined_inventory is a python script that gathers data from a Netbox source-of-truth and stores them as Ansible inventory, group_vars and host_vars files.
GNU General Public License v3.0
22 stars 12 forks source link

Alternative netbox.netbox.nb_inventory_inventory #5

Open ypid opened 3 years ago

ypid commented 3 years ago

It looks to me that this script got deprecated by https://netbox-ansible-collection.readthedocs.io/en/latest/plugins/inventory/nb_inventory/netbox.netbox.nb_inventory_inventory.html. The reason I looked into netbox-joined-inventory was that it provides a nice way to generate inventory files that one can review. I have now tested a way to get a similar behavior with netbox.netbox.nb_inventory:

ansible/inventory/netbox.yml:

---
# vim: foldmarker=[[[,]]]:foldmethod=marker

plugin: netbox.netbox.nb_inventory
token: invalid
## More netbox.netbox.nb_inventory settings.

## 60 * 60 * 24 * 360 * 1000
cache_timeout: 31104000000

ansible/refresh-netbox.yml:

---
# vim: foldmarker=[[[,]]]:foldmethod=marker

plugin: netbox.netbox.nb_inventory
## More netbox.netbox.nb_inventory settings.

cache_timeout: 10

ansible.cfg:

[inventory]
cache = True
cache_plugin = jsonfile
cache_connection = ~/.ansible/inventory_cache

Makefile:

.PHONY: refresh-netbox
refresh-netbox: ansible/inventory/netbox.yml
    @echo "## This file is managed by a Makefile, all changes will be lost.\n" > ansible/refresh-netbox.yml
    sed 's/^cache_timeout:.*/cache_timeout: 10/;/^token: invalid$$/d;s/^# real token:/token:/;' "$<" >> ansible/refresh-netbox.yml
    ansible-inventory -v --list -i ansible/refresh-netbox.yml | less
    git -C ~/.ansible/inventory_cache diff
    git -C ~/.ansible/inventory_cache add .
    git -C ~/.ansible/inventory_cache commit -m "Auto commit after review"

To refresh the cache, run ansible-inventory -v --list -i ansible/refresh-netbox.yml and then review the changes in ~/.ansible/inventory_cache or make refresh-netbox.

You could also use ansible-playbook --diff --check in combination with manual cache refresh.

What do you think?

marioland commented 3 years ago

If I understood the netbox.netbox.nb_inventory right it is able to generate a dynamic inventory in a generic way. With your addition it would also be able to export the inventory to files.

On the other hand the netbox-joined-inventory is part of a networking stack that is able to configure switches. It can take all VLAN, VXLAN, Anycast, MLAG , BGP data and more all from netbox. Therefore netbox-joined-inventory needs to join a lot of tables from netbox. See https://github.com/Sidarion/netbox-joined-inventory data model. After all the joining netbox-joined-inventory will produce the static files that ansible can take as inventory and all details in the detailed host_vars files.

As all this joining is very specific to this networking stack I doubt that the more generic netbox.netbox.nb_inventory could replace the netbox-joined-inventory.

ypid commented 3 years ago

Right. That is the thing I was not sure about, how difficult it would be to implement your use case with the generic inventory. From the data model and the implementation it looks like explicit IPAM access is not provided by netbox.netbox.nb_inventory. But my question would be if you really need that as the information relevant to a device is included in the inventory of that device (VLAN names, VIDs, IP addresses of the interfaces). Also custom fields of devices are included. But I don’t need an answer to that as my use case is not a complex network. I just wanted to provide input how the review can be implemented with the official inventory plugin.

Thanks again for sharing all this work!