[FEATURE REQUEST] Custom path for group_vars and host_vars in ansible.cfg

Hi all,

I’ve been wondering on how am I going to move the contents of my group_vars and host_vars to a different location then I found https://github.com/ansible/ansible/issues/13051 which basically was rejected.

My use case is mainly to have a better organization of my configurations. Since I was able to specify a custom location for my inventory and roles_path as stated also in the above link, I also wanted to set a custom location for my group_vars and host_vars.

Currently, my Ansible directory tree looks like this:

ANSIBLE_DIR=/etc/ansible

$ANSIBLE_DIR/ansible.cfg
$ANSIBLE_DIR/main.yml
$ANSIBLE_DIR/environment/{development,staging,production}
$ANSIBLE_DIR/roles/{defaults,files,handlers,meta,tasks,templates,vars}/

Then, I want to have my group_vars and host_vars to be located at:

$ANSIBLE_DIR/vars/{groups,hosts}/

I find it messy (sorry) if my group_vars and host_vars are located inside the $ANSIBLE_DIR/environment/ directory. Thus, I want to move it to some other place.

Thank you and I hope you will consider this feature request.

Kind regards,

Muffin

Hi All,
As much as I hate to bring an old thread back to life, I have to as I’ve failed to find workarounds for this issue.
Based on my inventory file, I would like to keep the group_vars dir in multiple locations instead of the same single directory as it makes it harder to maintain.
So instead of

group_vars
├── db_in
├── db_servers
├── db_us
├── in_servers
├── us_servers
├── web_in
├── web_servers
└── web_us

I would like to have something like (or any other suggestions/workaround to make things more organised) below:

group_variables
├── db-servers-grp
│ ├── db_in
│ └── db_us
├── general-grp
│ ├── db_servers
│ └── web_servers
├── region-grp
│ ├── in_servers
│ └── us_servers
└── web-server-grp
├── web_in
└── web_us

This way I can have in my ansible.cfg file the following variable:

group_vars_dirs = group_variables/db-servers-grp, group_variables/general-grp, group_variables/region-grp, group_variables/web-server-grp

This would help in scalability when there are thousands of regions or even sub-regions
I have an inventory as below (This is a sample of a larger inventory of around 2000 hosts):

all:
children:
web_servers:
children:
web_us:
hosts:
web_us_example1:
web_us_example2:
web_us_example3:
web_in:
hosts:
web_in_example1:
web_in_example2:
web_in_example3:
db_servers:
children:
db_us:
hosts:
db_us_example1:
db_us_example2:
db_us_example3:
db_in:
hosts:
db_in_example1:
db_in_example2:
db_in_example3:
us_servers:
children:
web_us:
db_us:
in_servers:
children:
web_in:
db_in:

I would appreciate any feedback at all.

Thanks,
Rahul Raj Purohit

You can just develop your own "vars plugin" that does this, the
current host_vars/group_vars is implemented via the host_group_vars
plugin included in core
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/host_group_vars_vars.html.
You can even disable the one shipped with core in favor of your own.