---
title: "Ansible and the relations to the inventory"
date: 2020-08-03T22:45:00+02:00
aliases:
  - /posts/ansible-and-the-relations-to-the-inventory/
authors:
  - robert-kaussow
tags:
  - Sysadmin
  - Automation
resources:
  - name: feature
    src: "images/feature.jpg"
    params:
      anchor: Center
      credits: >
        [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.

<!--more-->

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 type=note >}}
**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:

<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<!-- spellchecker-disable -->
{{< highlight bash "linenos=table" >}}
├── group_vars
│   └── all.yml
├── host_vars
│   └── ...
├── inventory
└── playbooks
    └── ...
{{< /highlight >}}
<!-- spellchecker-enable -->
<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

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:

<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<!-- spellchecker-disable -->
{{< highlight bash "linenos=table" >}}
├── group_vars
│   └── all.yml
├── host_vars
│   └── ...
├── inventory
│   ├── dynamin_script.py
│   └── static
└── playbooks
    └── ...
{{< /highlight >}}
<!-- spellchecker-enable -->
<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

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?
