thegeeklab/content/posts/2020/ansible-and-the-relations-t.../index.md

71 lines
2.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
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 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:
<!-- 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?