Ansible Dynamic Inventory
♦️

Ansible Dynamic Inventory

Created
Feb 5, 2022 1:22 AM
Tags
devopsinfrastructure

At Mighty, we use Ansible for a lot of our server automation. It has worked really well for us so far! One of the things that’s been frustrating me lately about Ansible is our use of static inventory files. Ansible inventory is the fleet of servers that you wish to be able to run commands on.

Having a static file means that whenever there is a change to the database record of our servers, we’d need to also manually update a static file in our repo. As we continue to scale, this is getting less and less manageable. Files also have a worse read/write interface than a SQL database. To solve the problem, I built an integration between our infrastructure web server which manages our virtual machines and hypervisors with Ansible inventory.

Ansible has support for dynamic inventory (https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.html) and all it took was for me to build a Python script that queried the database source rather than using the static file for our inventory. By doing this, we were able to get real time information on whether a server was available or not, what user is on it, and more. It reduces the risk of stale data and the tech debt associated with having to update multiple data sources when any need to be changed. Additionally, data duplication is prone to error. My particular concern is that In my opinion, its better to have one source of truth for all data and to use that source for as long as it is scaleable. A database often fits the bill here since it has an easy to read/write interface and some databases also have ACID guarantees.