Ansible and the relations to the inventory

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…

Pro tip
There is one thing you should keep in mind, variables are related to the inventory file(s) or Playbooks.

Let’s assume your Ansible structure looks like this:

1
2
3
4
5
6
7
├── group_vars
│   └── all.yml
├── host_vars
│   └── ...
├── inventory
└── playbooks
    └── ...

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:

1
2
3
4
5
6
7
8
9
├── group_vars
│   └── all.yml
├── host_vars
│   └── ...
├── inventory
│   ├── dynamin_script.py
│   └── static
└── playbooks
    └── ...

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?