Skip to content

The interplay between host_vars and group_vars dirs and inventory dirs is not fully documented #2462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kpinc opened this issue Mar 20, 2025 · 7 comments
Assignees

Comments

@kpinc
Copy link

kpinc commented Mar 20, 2025

Hi,

This is more-or-less a review of PR #2416, with an eye towards what PR #2386 was intended to improve. Apologies for doing this after-the-fact.

In no particular order:

Nothing explicitly says that when you specify a directory in which to find inventory, that inventory is collected in a recursive breath-first manner from all sub-directories. Although this is implied by "Files will be read and loaded in alphabetically sorted order from the top directory down." , the reader has to pay attention, and do a certain amount of assuming, to realize that that means that sub-directories, and their sub-sub-directories, etc., are searched for inventory files. Something more explicit would be better.

Nothing explicitly says that host_vars and group_vars dirs are searched for only in the directory given to ansible as the top-level dir in which to search for inventory. This is almost implied by an example that starts with "If your inventory file at /etc/ansible/hosts ...". But not really because I can invoke ansible with
ansible-playbook -i /etc ... and while the example will still use the same inventory files, the group_vars and host_vars dirs will not be used. (And yes, looking through all of /etc would be bad, but that's not the point.) The example implies that it matters where the inventory file is found, but this is not the case. In the end, it is left undocumented that, although inventory files are searched for in sub-directories, this is not the case for the host_vars and group_vars directories.

I am not a fan of "documentation by example". I believe that the text should be explicit about the behavior documented. Rather than just an example it would be best to have sentences that document behavior. So the example starting with "If your inventory file at /etc/ansible/hosts ..." should be supplemented with sentences that document behavior. The example can then clarify and illustrate the behavior. The reader should not have to guess at the behavior the example is supposed to illustrate.

@ansible-documentation-bot ansible-documentation-bot bot added the needs_triage Needs a first human triage before being processed. label Mar 20, 2025
@oraNod oraNod removed the needs_triage Needs a first human triage before being processed. label Mar 25, 2025
@oraNod
Copy link
Contributor

oraNod commented Mar 25, 2025

@bcoca I've assigned this to you since it follows up on one of your PRs. Hope that's OK. Could you please take a look and provide your thoughts? Thanks.

@bcoca
Copy link
Member

bcoca commented Mar 25, 2025

so

The host_group_vars vars plugin loads host and group variable files by searching paths relative to the inventory source or the playbook file. If your inventory file at /etc/ansible/hosts contains a host named ‘foosball’ that belongs to two groups, ‘raleigh’ and ‘webservers’, that host will use variables in YAML files at the following locations:

to

The host_group_vars vars plugin loads host and group variable files by searching paths adjacent to the inventory source or the playbook file. If your inventory file at /etc/ansible/hosts contains a host named ‘foosball’ that belongs to two groups, ‘raleigh’ and ‘webservers’, that host will use variables in YAML files at the following locations:

?

@kpinc
Copy link
Author

kpinc commented Mar 26, 2025 via email

@bcoca
Copy link
Member

bcoca commented Mar 26, 2025

I changed 'relative' to 'adjacent' which I believe clarifies that the source must be at the same level

@kpinc
Copy link
Author

kpinc commented Mar 26, 2025 via email

@bcoca
Copy link
Member

bcoca commented Mar 26, 2025

why the documentation makes a distinction between 'inventory sources' vs 'inventory files', the first is what you supply to ansible as a 'source' and becomes the basis for search for vars plugins, the 2nd has actual 'content' for the plugins to consume.

@kpinc
Copy link
Author

kpinc commented Mar 27, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 Triage
Development

No branches or pull requests

3 participants