Ansible, is group_vars and host_vars dirs at multiple levels supported?

Viewed 42

I am experimenting with variable overriding in Ansible. To do so, I have created the below-depicted directory structure. Note that under inventories, I have created two separate sites (1 & 2) Also, note that I have added group_vars/host_vars at two different levels; below inventories and each site.

.
├── ansible.cfg
├── inventories
│   ├── group_vars
│   │   └── all.yml
│   ├── host_vars
│   │   └── target2.yml
│   ├── site1
│   │   ├── group_vars
│   │   │   └── all.yml
│   │   ├── host_vars
│   │   │   └── target1.yml
│   │   └── hosts.yml
│   └── site2
│       ├── group_vars
│       │   └── all.yml
│       ├── host_vars
│       │   └── target2.yml
│       └── hosts.yml
├── modules
├── playbooks
│   └── playbook1
│       ├── group_vars
│       │   └── all.yml
│       └── host_vars
└── roles

I would like to be able to store default variables for groups/hosts at "inventories" level and override them when/if necessary at site/group/host level using directories (not the hosts.yml), but I am unable to do so.

If I test the inventory by targeting the base "inventories" directory, I can see that group_vars/host_var folders under sites are ignored:

ansible-inventory --vars --graph -i inventories/

@all:
  |--@site1:
  |  |--target1
  |  |  |--{scope = inventories/site1/hosts.yml}
  |  |--target2
  |  |  |--{scope = inventories/host_vars/target2.yml}
  |--@site2:
  |  |--target2
  |  |  |--{scope = inventories/host_vars/target2.yml}
  |--@ungrouped:
  |--{scope = inventories/group_vars/all.yml}

But if I target a specific site, the underlying group_vars/host_var folder are used, but of course the one at base "inventory" are ignored:

ansible-inventory --vars --graph -i inventories/site1

@all:
  |--@site1:
  |  |--target1
  |  |  |--{scope = inventories/site1/host_vars/target1.yml}
  |  |--target2
  |  |  |--{scope = inventories/site1/group_vars/all.yml}
  |--@ungrouped:
  |--{scope = inventories/site1/group_vars/all.yml}

ansible-inventory --vars --graph -i inventories/site2

@all:
  |--@site2:
  |  |--target2
  |  |  |--{scope = inventories/site2/host_vars/target2.yml}
  |  |--{scope = inventories/site2/group_vars/all.yml}
  |--@ungrouped:

Is it possible to instruct ansible to look for group_vars/host_var folders in the entire directory structure?

Thanks!

1 Answers

According your description and example your sets site1 and site2 are already subsets of set all. You made the observation and described it in your question

If I target a specific site, the underlying group_vars/host_var folder are used, but of course the one at base "inventory" are ignored

also the used command gives a hint

ansible-inventory --vars --graph -i inventories/site1 # or 2

since with this you'll set the root of a tree structure or graph to a sub tree or part of a graph.

Is it possible to instruct Ansible to look for group_vars/host_var folders in the entire directory structure?

No, since there will be not other directory outside of the defined subset, sub tree or graph part.

but of course the one at base "inventory" are ignored

In other words, the base "inventories" doesn't exists (anymore) since you've set the base to "inventories/site1" or 2.


An other approach could be to have one inventory for each site.

Related