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/*.
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.
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.
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.
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.
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 that I think does what you want without a recursive search.