# Creating runtime dynamic inventory - How to use group\_vars from different stages

**URL:** <https://forum.ansible.com/t/creating-runtime-dynamic-inventory-how-to-use-group-vars-from-different-stages/46382>\
**Category:** Get Help\
**Tags:** playbook, aap, awx, ansible-core\
**Created:** [September 30, 2026, 7:57pm UTC](https://forum.ansible.com/t/creating-runtime-dynamic-inventory-how-to-use-group-vars-from-different-stages/46382 "2026-09-30T19:57:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://forum.ansible.com/u/Jonny)\
**Post date:** [September 30, 2026, 7:57pm UTC](https://forum.ansible.com/t/creating-runtime-dynamic-inventory-how-to-use-group-vars-from-different-stages/46382/1 "2026-09-30T19:57:11Z")

</div>

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.

```auto
## 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!

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [September 30, 2026, 8:59pm UTC](https://forum.ansible.com/t/creating-runtime-dynamic-inventory-how-to-use-group-vars-from-different-stages/46382/2 "2026-09-30T20:59:13Z")

</div>

> [@Jonny](#):
>
> `inventory: inventories/prod/inventory.yml,inventories/dev/inventory.yml` must be set in `ansible.cfg`

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:

```auto
## 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:

```auto
[prod]
some-prod-host

```

specified inside and similar for dev.

---

<div class="post-metadata">

**Author:** ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://forum.ansible.com/u/Jonny)\
**Post date:** [October 1, 2026, 7:02am UTC](https://forum.ansible.com/t/creating-runtime-dynamic-inventory-how-to-use-group-vars-from-different-stages/46382/3 "2026-10-01T07:02:26Z")

</div>

When running the script, would the -i parameter be specified, and would the group variables be loaded correctly?

I specified this in ansible.cfg so I don’t have to specify -i every time I run the script. But as I said, all group variables are then automatically loaded.

Just for your understanding, the following code is written inside at hosts.conf.j2:

```auto
---
plugin: netbox.netbox.nb_inventory
validate_certs: false
config_context: true
flatten_config_context: true
flatten_custom_fields: true
fetch_all: false
interfaces: true
query_filters:
  - role: server_role
  - status: planned
  - status: active
  - status: staged
  - status: failed
  - status: decommissioning
  - tag: {{ tag }}
group_by:
  - tags
  - device_roles
  - services
group_names_raw: true
api_endpoint: {{ url }}
token: {{ token }}

```

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [October 1, 2026, 3:10pm UTC](https://forum.ansible.com/t/creating-runtime-dynamic-inventory-how-to-use-group-vars-from-different-stages/46382/4 "2026-10-01T15:10:06Z")

</div>

> But as I said, all group variables are then automatically loaded.

I don’t understand what this means or what the expected behavior is. Are the prod hosts loading dev group\_vars (or vice versa)?

It sounds like the same host is in multiple groups. You can check with `ansible-inventory --graph`. It might be a better fit to use `include_vars` to conditionally include variables. You could possibly make `group_vars` work as you expect by using unique host names to avoid host deduplication/merging of those groups (multiple distinct hosts can define the same `ansible_host`, but hosts that define the same `inventory_hostname` reference the same object).

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [October 1, 2026, 9:18pm UTC](https://forum.ansible.com/t/creating-runtime-dynamic-inventory-how-to-use-group-vars-from-different-stages/46382/5 "2026-10-01T21:18:00Z")

</div>

> [@Jonny](#):
>
> When running the script, would the -i parameter be specified, and would the group variables be loaded correctly?
> 
> I specified this in ansible.cfg so I don’t have to specify -i every time I run the script. But as I said, all group variables are then automatically loaded.

First, the behavior you are seeing is intended. Just to make it clear. You are misunderstanding how inventory is loaded and processed.

Second, when you specify something like `inventory: inventories/prod/inventory.yml,inventories/dev/inventory.yml` in `ansible.cfg` this is implicitly the same as calling the command like:

```auto
ansible-playbook -i inventories/prod/inventory.yml -i inventories/dev/inventory.yml myplaybook.yml

```

Note the two times `-i` option.

When multiple inventories are passed to Ansible, it merges them into one big inventory. This means that:

1. All the hosts from all the inventories are assembled on one big pile. If there are some duplicated hosts, only the last one that is read will be the valid one. The previous ones will be overwritten. Load order of inventory files is alphabetical.
2. All groups are assembled on one big pile and if groups are repeated in multiple inventory files, they will be merged - i.e. list of hosts in a group will be added to one another.
3. Every group var will be applied to all hosts in a group, meaning, it will be applied to all hosts from all inventory files that are specified as members of that group (since groups themselves are merged).

In other words, everything becomes global across all the inventory files. There are no more two distinct inventories, just two parts of the same inventory.

Since you are using the inventory plugin, the NetBox one, it a little bit harder to understand what happens under the hood. Your best option is to use `ansible-inventory` tool for troubleshooting.

Regarding the `-i` option, if you explicitly specify it in a command line, Ansible will only read that one explicitly specified inventory. So when you do `-i inventories/dev/inventory.yml` it will load only dev hosts and only group vars specified in dev inventory.

I wonder what is the difference between `hosts.conf.j2` in dev and prod inventory?

I see that you have:

```auto
group_by:
  - tags

```

This means you can assign tag `dev` for dev hosts and tag `prod` for prod in NetBox and you will have `dev` and `prod` groups automatically defined and populated. After that, you can use the folder layout as described in my previous message to divide variables per groups.

---

<div class="post-metadata">

**Author:** ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://forum.ansible.com/u/Jonny)\
**Post date:** [October 6, 2026, 8:40am UTC](https://forum.ansible.com/t/creating-runtime-dynamic-inventory-how-to-use-group-vars-from-different-stages/46382/6 "2026-10-06T08:40:24Z")

</div>

Thanks for the answering. I understand it more now and recognised that I made some mistakes.  
Sometimes it’s good to talk to somebody to understand things better.
