# idea: allow group\_vars/ and host\_vars/ files to be organised in sub folders

**URL:** <https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672>\
**Category:** Ansible Project\
**Created:** [April 12, 2013, 7:51am UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672 "2013-04-12T07:51:14Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [April 12, 2013, 7:51am UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672/1 "2013-04-12T07:51:14Z")

</div>

Hi list,

My use case:

At $WORK, we divise our ansible repo in multiple projects/\* dirs, where all projects have their own hosts file and group\_vars/; a wrapper script merges all these together in $basedir; basically having all group\_vars files in one big directory is too big here (we have 800 hosts and almost 300 groups).  
(This should better be handled by some backend + inventory script, but for now, that’s what we have. )

Now, one optimisation, would be for us to get rid of that part in the wrapper script, basically allowing to organise goup\_vars/ and host\_vars/ files into subdirectories.

Basically what I want is, instead of having

group\_vars/

group\_vars/project1group1

group\_vars/project1group2

group\_vars/project2group1

group\_vars/project2group2

I want to have this:

group\_vars/

group\_vars/project1/project1group1

group\_vars/project1/project1group2

group\_vars/project1/project2group1

group\_vars/project1/project2group2

The only problem I see with that, is that would allow to have multiple files for the same group, such as

group\_vars/

group\_vars/project1/project1group1

group\_vars/project2/project1group1

Which would probably confusing, and not clear which logic is implemented on which variable gets precedence when that same variable is defined in multiple files with the same group name, hence for the same group.  
(in our case, we just wouldn’t have that, by convention, but it would be possible to abuse)

group\_vars/ and host\_vars/ are handled by the vars\_plugins/group\_vars plugin, and looking at the code, that could be done with a small and simple patch.

Your ideas?

Serge

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 12, 2013, 1:53pm UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672/2 "2013-04-12T13:53:16Z")

</div>

How would ansible know which subdirectory to use?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [April 12, 2013, 1:56pm UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672/3 "2013-04-12T13:56:30Z")

</div>

​It doesn't have to, just parse all the files it can find.  
The intermediatie $project directory is just for organisational purposes.

As you said on irc, parsing all leaf nodes with matching filenames.

&nbsp;&nbsp;Serge​

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 12, 2013, 2:54pm UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672/4 "2013-04-12T14:54:10Z")

</div>

I’m fine with this, we should make it also ignore file extensions at the same time.

---

<div class="post-metadata">

**Author:** ![Govinda\_Fichtner](https://avatars.discourse-cdn.com/v4/letter/g/3e96dc/32.png) [@Govinda\_Fichtner](https://forum.ansible.com/u/Govinda_Fichtner)\
**Post date:** [July 23, 2014, 4:01pm UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672/5 "2014-07-23T16:01:11Z")

</div>

Is this already implemented in a current Ansible version?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [July 23, 2014, 5:15pm UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672/6 "2014-07-23T17:15:26Z")

</div>

​Yes, instead of e.g. a group\_vars/groupname.yml, you can​ now have  
group\_vars/groupname/ directory with multiple files in it.

---

<div class="post-metadata">

**Author:** ![r.bettridge](https://avatars.discourse-cdn.com/v4/letter/r/bbe5ce/32.png) [@r.bettridge](https://forum.ansible.com/u/r.bettridge)\
**Post date:** [August 7, 2015, 6:17pm UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672/7 "2015-08-07T18:17:29Z")

</div>

I just tried this on 1.9.1 I have /group\_vars/staging/staging.yml and /group\_vars/staging/secrets.yml and it seems to me that it is not merging the files properly.

I have an api variable in the staging file which doesnt exist in the secrets file. When I run the playbook it complains that the api variable doesnt exist. If I either remove the secrets file or add the api variable to the secrets file then it works.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [August 8, 2015, 8:58pm UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672/8 "2015-08-08T20:58:54Z")

</div>

​If that would be true it would be a bug, and you would need to present us  
a minimal example on how to reproduce it.

That being said, I use ​this feature everywhere around and i know i works.  
It might be some corner case of course, which would only become clear when  
being able to reproduce it.

Can you make a minimal example that reproduces this, and show us the needed  
files?

Thanks,

Serge

---

<div class="post-metadata">

**Author:** ![Sean\_Cavanaugh1](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Sean\_Cavanaugh1](https://forum.ansible.com/u/Sean_Cavanaugh1)\
**Post date:** [September 10, 2016, 6:02pm UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672/9 "2016-09-10T18:02:22Z")

</div>

I am curious if there was follow-up to this. I am seeing something similar for a customer I am working on.

- If I put all my variables under group\_vars/all (where all is a flat file in yml syntax) it works fine.

- If I move one device out of group\_vars/all to group\_vars/leaf/leaf01 all devices work great. (where leaf corresponds to a group within my Ansible hosts file).

- As soon as I do group\_vars/leaf/leaf01 and leaf02 it can’t find leaf01 anymore. leaf02 will work but not leaf01. (where leaf01 and leaf02 are hosts in my ansible hosts file both in the group leaf)  
Do they have to end with .yml? Is this not how group\_vars works? Just trying to make folders to break up variables for simplification.  
(I am running on Ansible 2.1.1)

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [September 11, 2016, 6:11pm UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672/10 "2016-09-11T18:11:36Z")

</div>

Are you sure that's the way things are supposed to work?

I'd have expected individual hosts to be under host\_vars/leaf01 , not  
nested under a group (remember, a host can be in multiple groups).

so your layout would be more:

.  
├── group\_vars  
│ ├── all  
│ └── leaf  
├── host\_vars  
│ ├── leaf01  
│ └── leaf02  
└── hosts

---

<div class="post-metadata">

**Author:** ![Sean\_Cavanaugh1](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Sean\_Cavanaugh1](https://forum.ansible.com/u/Sean_Cavanaugh1)\
**Post date:** [September 12, 2016, 1:15pm UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672/11 "2016-09-12T13:15:33Z")

</div>

In this thread though Dehaan said this should work-\>

> I just tried this on 1.9.1 I have /group\_vars/staging/staging.yml and /group\_vars/staging/secrets.yml and it seems to me that it is not merging the files properly.

But others said this was a problem. I just don’t think this is documented well. If its not supposed to work I guess it shouldn’t work ☹

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 14, 2016, 6:04am UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672/12 "2016-09-14T06:04:31Z")

</div>

According to the documentation it should work  
[https://docs.ansible.com/ansible/intro\_inventory.html#splitting-out-host-and-group-specific-data](https://docs.ansible.com/ansible/intro_inventory.html#splitting-out-host-and-group-specific-data)

Quote from the page:  
"As an advanced use-case, you can create directories named after your groups or hosts, and Ansible will read all the files in these directories. An example with the ‘raleigh’ group:

&nbsp;&nbsp;&nbsp;/etc/ansible/group\_vars/raleigh/db\_settings  
&nbsp;&nbsp;&nbsp;/etc/ansible/group\_vars/raleigh/cluster\_settings

All hosts that are in the ‘raleigh’ group will have the variables defined in these files available to them. This can be very useful to keep your variables organized when a single file starts to be too big, or when you want to use Ansible Vault on a part of a group’s variables. Note that this only works on Ansible 1.4 or later."

---

<div class="post-metadata">

**Author:** ![Andrea\_Dell\_Amico](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@Andrea\_Dell\_Amico](https://forum.ansible.com/u/Andrea_Dell_Amico)\
**Post date:** [September 14, 2016, 7:38am UTC](https://forum.ansible.com/t/idea-allow-group-vars-and-host-vars-files-to-be-organised-in-sub-folders/12672/13 "2016-09-14T07:38:03Z")

</div>

> > In this thread though Dehaan said this should work-\>
> > 
> > > I just tried this on 1.9.1 I have /group\_vars/staging/staging.yml and  
> > > /group\_vars/staging/secrets.yml and it seems to me that it is not merging  
> > > the files properly.
> > 
> > But others said this was a problem. I just don't think this is documented  
> > well. If its not supposed to work I guess it shouldn't work ☹
> 
> According to the documentation it should work  
> [https://docs.ansible.com/ansible/intro\_inventory.html#splitting-out-host-and-group-specific-data](https://docs.ansible.com/ansible/intro_inventory.html#splitting-out-host-and-group-specific-data)

And it does. I use this feature heavily since before ansible 1.9, never had a problem.  
Be careful to not duplicate variables definitions. Starting from version 2 ansible issue a warning if a variable has been defined more than once, but if I remember well it was silent on older versions.

> --  
> Kai Stian Olstad

Ciao  
Andrea
