Hello community,
In an Ansible project, I have a dynamic inventory generated at runtime using a Jinja2 template, which I then load using refresh inventory. The inventory is generated because I use different environments that are automatically retrieved from the SSoT. My Ansible code is written for the CLI, and I don’t want to use AAP, AWX, or anything else.
Now I’m facing the problem that I want to configure different environments with it—specifically, a PROD and a DEV environment with different group_vars.
For the inventory to be generated correctly, the parameter inventory: inventories/prod/inventory.yml,inventories/dev/inventory.yml must be set in ansible.cfg. Unfortunately, this is where I run into a problem. Setting both inventory paths causes all group_vars to be loaded.
## Folder Layout
inventories/
prod/
hosts.conf.j2 # inventory file for prod servers
group_vars/
y.yml
host_vars/
x.yml
dev/
hosts.conf.j2 # inventory file for dev environment
group_vars/
y.yml
host_vars/
x.yml
The tasks used to create the runtime dynamic inventory were module, template, copy, set_fact, and meta. I’ll skip writing these tasks here unless they’re specifically requested.
Has anyone ever encountered a similar problem or come up with a better solution for my setup?
Thanks for the help!