Welcome, Guest. Please login or register.

Author Topic: Dynamic Ansible inventory from NeDi  (Read 3231 times)

Westy_87

  • Newbie
  • *
  • Posts: 14
    • View Profile
Dynamic Ansible inventory from NeDi
« on: October 28, 2021, 07:01:34 am »
Hi All,

This may better be suited to the database forum but since it's about integration, I've left it here.

We use Ansible every now and then for certain configuration rollouts and information gathering. Currently we have an inventory file setup that links hostnames to IP addresses. The problem with this is it requires manual changes whenever hostnames or IP addresses change, get added or deleted. We have over 500 Cisco IOS and NXOS devices in this list and so it isn't always accurate which can cause issues.

I am trying to work out if there is a way to extract hostnames and management IP addresses from the NeDi database into a file that is either already in the correct format, or can be manipulated through some other means to then be in the correct format, for the Ansible inventory file.

My first hurdle is figuring out how to get the IP address in a DB query to show as a proper IP. Currently they look like '180432786' or such.

Then I need to get the output of a query that gives hostname and IP to look like:
<hostname> ansible_host=<x.x.x.x> mgt_ip=<x.x.x.x>

And then order and group the hosts into sites. The inventory file is structured like this:

[network:children]
<site1>
<site2>
<site3>

[site1:children]
site1_core
site1_distribution
site1_access

[site1_core]
<site1_core_switch1 hostname> ansible_host=<x.x.x.x> mgt_ip=<x.x.x.x>

[site1_distribution]
<site1_distribution_switch1 hostname> ansible_host=<x.x.x.x> mgt_ip=<x.x.x.x>

[site1_access]
<site1_access_switch1 hostname> ansible_host=<x.x.x.x> mgt_ip=<x.x.x.x>
<site1_access_switch2 hostname> ansible_host=<x.x.x.x> mgt_ip=<x.x.x.x>

etc

I really hope someone has some tips for me with this! I think if I can get the output of a database query to give me hostnames and IP's, ordered by hostname alphabetically, that would be a good start. Then I can try and manipulate this data through some other method to order it and add the text required for ansible inventory file.

joserra

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Dynamic Ansible inventory from NeDi
« Reply #1 on: October 29, 2021, 09:13:30 am »
Hi

You can use INET_NTOA function to obtain IP address from database.
Use Nedi option System / Database to launch your query.
For example :

  select device, INET_NTOA(devip) from devices;

Westy_87

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Dynamic Ansible inventory from NeDi
« Reply #2 on: November 09, 2021, 02:30:54 am »
Hey thanks so much, that was annoying me so much!

Westy_87

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Dynamic Ansible inventory from NeDi
« Reply #3 on: March 17, 2023, 12:37:04 am »
Oh and I got this working pretty easily with Python.

My python code queries the nedi database and extracts the list of switches and their management IP. Then I do some magic with it and output in format for the Ansible hosts file.

I also have one that generates a .ini file for a list of hosts in MobaXterm to easily SSH to a switch.

Let me know if you would want a copy of my python code. I'd give you a github link but I haven't got around to using github yet. I'm a programming hack!

rickli

  • Administrator
  • Hero Member
  • *****
  • Posts: 2901
    • View Profile
    • NeDi
Re: Dynamic Ansible inventory from NeDi
« Reply #4 on: March 23, 2023, 10:05:55 am »
cool beans ;)
Please consider Other-Invoices on your NeDi installation for an annual contribution, tx!
-Remo