# Inventory group\_vars loading behavior when inventory is directory

**URL:** <https://forum.ansible.com/t/inventory-group-vars-loading-behavior-when-inventory-is-directory/41454>\
**Category:** Get Help\
**Tags:** variables, inventory\
**Created:** [April 1, 2025, 7:15pm UTC](https://forum.ansible.com/t/inventory-group-vars-loading-behavior-when-inventory-is-directory/41454 "2025-04-01T19:15:54Z")\
**Posts on this page:** 11\
**Page:** 1

<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:** [April 1, 2025, 7:15pm UTC](https://forum.ansible.com/t/inventory-group-vars-loading-behavior-when-inventory-is-directory/41454/1 "2025-04-01T19:15:54Z")

</div>

So, I’ve experimented a bit with an inventory as directory, inventory group\_vars and dir/file hierarchy there of. What I have encountered is this:

```auto
/some/ansible/project
|-- playbook.yml # playbook
|-- group_vars # playbook level group_vars
| `-- somegroup # loaded
`-- inventory # inventory directory
    |-- group_vars # inventory group_vars
    | `-- somegroup # loaded
    `-- my_hosts # some arbitrary subdirectory
        |-- group_vars # inventory group_vars?
        | `-- somegroup # *ignored*!
        |-- somehost1 # loaded
        |-- somehost2 # loaded
        `-- somehost3 # loaded

```

The command line used is `ansible-playbook -i inventory/ playbook.yml`. Version used is 2.16.7.

Is this the expected behavior?

To be more clear. It seems that Ansible loads inventory group\_vars only from a `group_vars` directory that is a direct descendent (first sub directory) of the inventory directory. I was convinced that Ansible would load group\_vars from any `group_vars` directory anywhere down the hierarchy inside the inventory directory… and that it worked at some point 🤨 In other words, I expected Ansible to also load `inventory/my_hosts/group_vars/*`.

---

<div class="post-metadata">

**Author:** ![binbashroot](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@binbashroot](https://forum.ansible.com/u/binbashroot)\
**Post date:** [April 2, 2025, 1:04pm UTC](https://forum.ansible.com/t/inventory-group-vars-loading-behavior-when-inventory-is-directory/41454/2 "2025-04-02T13:04:18Z")

</div>

The host\_group\_vars vars plugin loads host and group variable files by searching paths relative to the inventory source or the playbook file. So your nested “group\_vars” under the my\_hosts diir would be ignored.

See:

> **[How to build your inventory — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#splitting-out-vars)**

---

<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:** [April 2, 2025, 1:17pm UTC](https://forum.ansible.com/t/inventory-group-vars-loading-behavior-when-inventory-is-directory/41454/3 "2025-04-02T13:17:56Z")

</div>

The documentation is not very clear on this. It does not specifically say that. It describes the behavior in case inventory is a file. In that case, `group_vars` is expected to be right beside inventory file in the same directory. On the other hand, when inventory is a directory, `group_vars` is expected to be **inside** an inventory directory.

My expectation was that when Ansible goes trough inventory directory, and parses each file it founds, that it looks for a `group_vars` and `host_vars` relative to each file found.

I’m struggling to understand the inventory parsing code in Ansible so any insight from someone who knows Ansible under the hood is very appreciated.

---

<div class="post-metadata">

**Author:** ![binbashroot](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@binbashroot](https://forum.ansible.com/u/binbashroot)\
**Post date:** [April 2, 2025, 1:48pm UTC](https://forum.ansible.com/t/inventory-group-vars-loading-behavior-when-inventory-is-directory/41454/4 "2025-04-02T13:48:51Z")

</div>

I agree that the documentation for using an inventory dir could be clearer. However, the rules still apply whether it’s a file or dir to my knowlegde. So your group\_vars that are relative to your diirectory would be applied. Also your “my\_hosts” dir has its own “relative” group\_vars which would apply. Your nested group\_vars of my\_hosts is not relative which is why it doesn’t get applied. I can’t speak to why. Maybe one of the devs can chime in here.

---

<div class="post-metadata">

**Author:** ![binbashroot](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@binbashroot](https://forum.ansible.com/u/binbashroot)\
**Post date:** [April 2, 2025, 1:50pm UTC](https://forum.ansible.com/t/inventory-group-vars-loading-behavior-when-inventory-is-directory/41454/5 "2025-04-02T13:50:43Z")

</div>

While this may not answer your question, it’s also a good resource re: inventories.

> **[Good Practices for Ansible - GPA](https://redhat-cop.github.io/automation-good-practices/#_inventories_and_variables_good_practices_for_ansible)**

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [April 2, 2025, 4:50pm UTC](https://forum.ansible.com/t/inventory-group-vars-loading-behavior-when-inventory-is-directory/41454/6 "2025-04-02T16:50:59Z")

</div>

Open issue about this [The interplay between host\_vars and group\_vars dirs and inventory dirs is not fully documented · Issue #2462 · ansible/ansible-documentation · GitHub](https://github.com/ansible/ansible-documentation/issues/2462)

---

<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:** [April 4, 2025, 11:57am UTC](https://forum.ansible.com/t/inventory-group-vars-loading-behavior-when-inventory-is-directory/41454/7 "2025-04-04T11:57:41Z")

</div>

Aaaahh, so this is definitely a known behavior. Thanks for sharing the link. My Googling ended up with nothing on this subject.

So the question then is whether this is intentionally implemented this way, or it unintentionally ended up this way, or no one really bothered thinking this way, or there was some technical or conceptual limitation… ?

Would anyone find the feature of being able to deeply nest inventory group\_vars and host\_vars useful?

What I’m trying to do here is to make a kind of composite inventory by gathering a bunch of independent inventories (unrelated Ansible projects), each with their own specific group\_vars, `inventory/my_hosts/group_vars` in the example. These specific group\_vars would be a selection of variables exported from the original projects. On the other hand, `inventory/group_vars` would then contain predefined collection of common variables, mostly considered read only, versioned and updated as a whole. `group_vars` in the root of the project would contain overrides for `inventory/group_vars`. In the current situation I have to move variables from `inventory/my_hosts/group_vars` to inventory files (i.e. `somehost1`, `somehost2`) in ini form `[group:vars]` to accomplish this.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [April 4, 2025, 2:30pm UTC](https://forum.ansible.com/t/inventory-group-vars-loading-behavior-when-inventory-is-directory/41454/8 "2025-04-04T14:30:48Z")

</div>

Yes, the current behavior is by design. There are a few ways forward to add the capability you want;

- create a new vars plugin that works like the included host\_group\_vars but adds recursive directory search (requires plugin + config)
- add an option to the current host\_group\_vars plugin to add this (but then you have to wait for release)
- add the option to a local copy that overrides host\_group\_vars plugin (requires updating plugin + local override)

---

<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:** [April 4, 2025, 3:01pm UTC](https://forum.ansible.com/t/inventory-group-vars-loading-behavior-when-inventory-is-directory/41454/9 "2025-04-04T15:01:01Z")

</div>

A recursive search would make host\_group\_vars so much slower potentially, and vars plugins can easily degrade performance because they run so often. Here’s [an example](https://gist.github.com/s-hertel/1fbfcb07f3e871a31c6eb7ff404406cf) that I think does what you want without a recursive search.

---

<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:** [April 8, 2025, 10:00pm UTC](https://forum.ansible.com/t/inventory-group-vars-loading-behavior-when-inventory-is-directory/41454/10 "2025-04-08T22:00:02Z")

</div>

Thanks for all the answers. I’ll have to take some time to consider what would be the best course of action for me.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 8, 2025, 10:00pm UTC](https://forum.ansible.com/t/inventory-group-vars-loading-behavior-when-inventory-is-directory/41454/11 "2025-05-08T22:00:26Z")

</div>

Closed on May 8, 2025, 10:00pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
