Host_vars not discovered

Hi I’ve followed Sample Ansible setup — Ansible Community Documentation and sometimes the host_vars directories are not discovered. How do I debug this, and does this layout work?

Yes.

By providing us with the reproducible example of what does not work :slightly_smiling_face:.

The documentation is kind of ambiguous about how to actually use that layout. I’m guessing it doesn’t work because you’ve configured the inventory source as inventories/. Try ansible-inventory -i inventories/production --list or ansible-inventory -i inventories/production/hosts - if the layout is the same as the example you should see any host_vars loaded from the production subdirectory. The host_group_vars plugin loads host_vars relative to inventory sources (for example, whatever you pass to -i, not to be confused with nested inventory files when the inventory source is a directory).

In case it’s helpful, I have an example of a custom vars plugin that loads vars relative to inventory files regardless of how the inventory sources are specified here.

Sadly I’m finding it to not be reproducible consistently hence why I asked, I was wondering if it was some old documentation that needed cleaning up.

What’s interesting on one machine doing ansible-playbook -i inventories/ site.yaml works (usually), but on a different machine it doesn’t. I’m happy to move to a different structure, though it is nice to be able to split up the inventory a bit more.

Keep in mind that the position of your playbook file in relation to group_vars and host_vars is also significant. If group_vars and host_vars are found next to the playbook file (in the same directory), they will be loaded as playbook level vars and have a higher precedence than vars defined at the inventory level (next to an inventory file or inside an inventory directory).

Additionally, there are (or were) some differences in how inventory directory structure is handled across different Ansible versions but that was mostly unintentional. What is guarantied is that in the inventory example from the documentation, passing -i inventories/production (inventory dir) or -i inventories/production/hosts (inventory file) will work and vars will be properly loaded. Passing the -i inventories/ will not load the vars as inventories/ is not a proper inventory directory. It’s just a directory holding multiple proper inventory directories. This is what @shertel pointed out.

Ah, it sounds like the machine this (usually) works on is on a version which has a “feature”/“bug” (depending on your perspective). I think rereading How to build your inventory — Ansible Community Documentation carefully I want to move to grouping via separate files on the same level rather than grouping into directories. I’ll do some tests and see if that works, then I’ll try to make the PR to make the docs clearer.

Thanks both!