thegeeklab/content/posts/ansible-and-the-relations-to-the-inventory/index.md
Robert Kaussow a1e956ed03
All checks were successful
continuous-integration/drone/push Build is passing
publish new post
2020-09-13 23:46:45 +02:00

2.8 KiB

title date authors tags resources
Ansible and the relations to the inventory 2020-08-03T22:45:00+02:00
robert-kaussow
Sysadmin
Automation
name src params
feature images/feature.jpg
anchor credits
Center [Sabri Tuzcu](https://unsplash.com/@sabrituzcu) on [Unsplash](https://unsplash.com/s/photos/coffee-headphone)

I love Ansible and I'm pretty happy to have this configuration management solution. But some little "features" drive me crazy sometimes. My task for today was to switch from a static Ansible inventory file to multiple dynamic inventory script. In general this, should be really straight forward. Create an inventory folder and add some small Python scripts to create Ansible readable inventories from different providers.

I've finished the first steps really quickly. After I've added a script for my home lab Proxmox VE host and a static inventory for a bunch of my Raspberries, I did a check with ansible-inventory -i inventory/ --list and everything seems to be working as expected. All my hosts were listed and Ansible groups were applied as well. But here comes the clue: The test Playbook run failed, it looks like Ansible was not able to find the required variables. What the heck...

{{< hint info >}} Pro tip
There is one thing you should keep in mind, variables are related to the inventory file(s) or Playbooks. {{< /hint >}}

Let's assume your Ansible structure looks like this:

{{< highlight bash "linenos=table" >}} ├── group_vars │   └── all.yml ├── host_vars │   └── ... ├── inventory └── playbooks    └── ... {{< /highlight >}}

Everything should work as expected, Ansible should be able to lookup the required group_vars and host_vars. After you switched to multiple inventories the structure might look this way:

{{< highlight bash "linenos=table" >}} ├── group_vars │   └── all.yml ├── host_vars │   └── ... ├── inventory │   ├── dynamin_script.py │   └── static └── playbooks    └── ... {{< /highlight >}}

At this point Ansible will fail and complain about missing variables. Well, as mentioned above, variables are related to the inventory file(s)! As inventory is no longer a single file, this requirement is not met anymore. To get it working again I had to symlink the group_vars and host_vars folder into the inventory folder. Obviously, right?