Creating runtime dynamic inventory - How to use group_vars from different stages

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!

Why does it have to be set in ansible.cfg? Feels like a wrong approach to me.

UPDATE: What you have effectively achieved is to have one big inventory split into two parts. Ansible combines them on load. Since that is the case, then you must separate your DEV and PROD hosts per group, not per inventory file. You will have group_vars file dev.yml and a group_vars file prod.yml assuming you called your groups dev and prod respectively. Hosts can be members of multiple groups so you don’t lose anything - you reuse the other groups as they are. Maybe an example will be more helpfull:

## Folder Layout
inventories/
   prod/
      hosts.conf.j2               # inventory file for prod servers
      group_vars/
         prod.yml
         some_additional_group.yml
      host_vars/
         some-prod-host.yml
   dev/
      hosts.conf.j2              # inventory file for dev environment
      group_vars/
         dev.yml
         some_additional_group.yml
      host_vars/
         some-dev-host.yml

For prod host.conf.j2, it must have an equivalent of:

[prod]
some-prod-host

specified inside and similar for dev.