# Emulating host\_vars and group\_vars for files

**URL:** <https://forum.ansible.com/t/emulating-host-vars-and-group-vars-for-files/18013>\
**Category:** Ansible Project\
**Created:** [April 18, 2015, 1:51pm UTC](https://forum.ansible.com/t/emulating-host-vars-and-group-vars-for-files/18013 "2015-04-18T13:51:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev2](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Anand\_Buddhdev2](https://forum.ansible.com/u/Anand_Buddhdev2)\
**Post date:** [April 18, 2015, 1:51pm UTC](https://forum.ansible.com/t/emulating-host-vars-and-group-vars-for-files/18013/1 "2015-04-18T13:51:12Z")

</div>

Dear Ansible developers and users,

I really like the groups\_vars and host\_vars concept of Ansible, and I was thinking of a way to emulate this for files. So here’s an example playbook:

- hosts: all  
tasks:
- debug: var=item  
with\_first\_found:
- paths:
- host\_files/{{ inventory\_hostname }}  
files:
- myfile
- paths: “{{ group\_names }}”  
files:
- myfile

This works just fine for a host-specific file, because there can only ever be one directory for a host. it also sort-of works for groups, but there are 2 complications:

1. the order of the names in group\_names does not match the inventory parent-child order; and
2. I don’t know how to prefix “group\_files” to each group name, in order to keep my group files in their own directory.

Can anyone suggest how I can achieve this? Or does this require a new custom plugin?

Anand

---

<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 18, 2015, 3:00pm UTC](https://forum.ansible.com/t/emulating-host-vars-and-group-vars-for-files/18013/2 "2015-04-18T15:00:50Z")

</div>

> 1. the order of the names in group\_names does not match the inventory  
> parent-child order; and

​There is no way of knowing that, as the inventory does not export that  
information to other ansible parts​

2. I don't know how to prefix "group\_files" to each group name, in order to

> keep my group files in their own directory.

perhaps I miss your point but

&nbsp;&nbsp;- paths: "group\_files/{{ group\_names }}"  
&nbsp;&nbsp;&nbsp;&nbsp;files:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- myfile

?​

---

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev2](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Anand\_Buddhdev2](https://forum.ansible.com/u/Anand_Buddhdev2)\
**Post date:** [April 18, 2015, 3:19pm UTC](https://forum.ansible.com/t/emulating-host-vars-and-group-vars-for-files/18013/3 "2015-04-18T15:19:09Z")

</div>

Hi Serge,

> > 1. the order of the names in group\_names does not match the inventory parent-child order; and
> 
> ​There is no way of knowing that, as the inventory does not export that information to other ansible parts​

Ah, that’s a bummer. Is there any way to get the inventory to expose this information for lookup plugins to use?

> > 1. I don’t know how to prefix “group\_files” to each group name, in order to keep my group files in their own directory.
> 
> perhaps I miss your point but
> 
> - paths: “group\_files/{{ group\_names }}”  
> files:
> - myfile

This can’t work. The “paths” attribute expects a list. By passing it “group\_files/{{ group\_names }}”, it turns into the string:

“group\_files/[‘group1’, ‘group2’, ‘group3’]”

This is a weird, but valid path, so ansible will look in there, but not find anything.

---

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev2](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Anand\_Buddhdev2](https://forum.ansible.com/u/Anand_Buddhdev2)\
**Post date:** [April 18, 2015, 3:37pm UTC](https://forum.ansible.com/t/emulating-host-vars-and-group-vars-for-files/18013/4 "2015-04-18T15:37:52Z")

</div>

I should have added that given the list a list [‘group1’, ‘group2’], I want to pass it to ansible as:

[‘group\_files/group1’, ‘group\_files/group2’]

---

<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 18, 2015, 5:20pm UTC](https://forum.ansible.com/t/emulating-host-vars-and-group-vars-for-files/18013/5 "2015-04-18T17:20:01Z")

</div>

> Hi Serge,
> 
> > > 1. the order of the names in group\_names does not match the inventory  
> > > parent-child order; and
> > 
> > ​There is no way of knowing that, as the inventory does not export that  
> > information to other ansible parts​
> 
> Ah, that's a bummer. Is there any way to get the inventory to expose this  
> information for lookup plugins to use?

​No. There is nothing in the api that allows this without an update to th​e  
core code.  
It is though something to consider, when we have a look at the inventory  
after 2.0.

> > 2. I don't know how to prefix "group\_files" to each group name, in order
> > 
> > > to keep my group files in their own directory.
> > 
> > perhaps I miss your point but
> > 
> > &nbsp;&nbsp;- paths: "group\_files/{{ group\_names }}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;files:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- myfile
> 
> This can't work. The "paths" attribute expects a list. By passing it  
> "group\_files/{{ group\_names }}", it turns into the string:
> 
> "group\_files/['group1', 'group2', 'group3']"
> 
> This is a weird, but valid path, so ansible will look in there, but not  
> find anything.

​Yes, sorry, I missed that obviously.​

​Slow Saturday here 🙂

This would require the possibility to redifine lists​. Allowing set\_fact to  
work with loops. Which is not possible either now, but will also hopefully  
become a thing post 2.0

Serge

---

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev2](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Anand\_Buddhdev2](https://forum.ansible.com/u/Anand_Buddhdev2)\
**Post date:** [April 18, 2015, 9:05pm UTC](https://forum.ansible.com/t/emulating-host-vars-and-group-vars-for-files/18013/6 "2015-04-18T21:05:40Z")

</div>

Hi Serge,

> ​No. There is nothing in the api that allows this without an update to th​e core code.
> 
> It is though something to consider, when we have a look at the inventory after 2.0.

Okay, thanks for confirming this.

The reason I have asked this question is that we’re using ansible in pull mode, with ansible running locally. This has the downside that all our roles, group\_vars and host\_vars are downloaded onto every host, thereby exposing some sensitive data to hosts that shouldn’t see it. Naturally, we cannot use the vault, because it requires password entry. And trying to figure out the roles that a host needs also appears undoable.

I have figured out a simple way to built rsync filters, that ensure that a pull-mode host only gets its own host\_vars file, and group\_vars files of the groups it belongs to. This has solved the issue of variables.

However, roles are a different story. We have several roles for different things, and many of these roles contain files that are only needed on one or a small subset of hosts. I would like to distribute these files only to the hosts that need them. This is the reason I asked my question.

For now, I have a work-around, that works, but is ugly. In my roles, wherever I need to copy a file, I’m doing this:

copy: content=‘{{ file\_content }}’ dest=/path/to/dest

I’m keeping the entire file’s content in a variable called “file\_content” in that host’s host\_vars file, or in a group\_vars file if appropriate. For lines of a few lines, this isn’t bad. But files that are bigger look ugly in a variable.

So if Ansible had the concept of host\_files and group\_files, with some kind of “first found” logic, it would be really nice.

Regards,  
Anand

---

<div class="post-metadata">

**Author:** ![Don\_Harper](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@Don\_Harper](https://forum.ansible.com/u/Don_Harper)\
**Post date:** [April 19, 2015, 2:54pm UTC](https://forum.ansible.com/t/emulating-host-vars-and-group-vars-for-files/18013/7 "2015-04-19T14:54:50Z")

</div>

Hum…could you make use of git or svn, and just checkout host specific branches? Might lead to a bit a bloat on the server side, but would keep the client side cleaner.

Don

---

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev2](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Anand\_Buddhdev2](https://forum.ansible.com/u/Anand_Buddhdev2)\
**Post date:** [April 19, 2015, 4:02pm UTC](https://forum.ansible.com/t/emulating-host-vars-and-group-vars-for-files/18013/8 "2015-04-19T16:02:19Z")

</div>

Hi Don,

Hum...could you make use of git or svn, and just checkout host specific

> branches? Might lead to a bit a bloat on the server side, but would keep  
> the client side cleaner.

We floated this idea early on in our design, and our entire team felt that  
a per-host branch would add too much overhead for us. Imagine managing a  
few hundred hosts, and having to switch branches for each host to make  
changes...

I gave this some more thought last night, and it turns out that many of the  
files we distribute are scripts or secret-less config files, and it's not  
too bad if they are exposed to all our hosts. The number of files that  
contain actual secrets are few, and the secrets are things like licence  
files, so my trick of keeping them in variables works well.

While Ansible was designed to be used in "push" mode, so that no state or  
sensitive data needs to be kept on managed servers, it also has the "pull"  
mode, which I really like, so we don't have to push anything. Clients check  
in when they can and reconfigure themselves. Therefore, something that  
would allow selective exposure of files and variables would be great!

Anand

---

<div class="post-metadata">

**Author:** ![Krzysztof\_Zarzycki](https://avatars.discourse-cdn.com/v4/letter/k/94ad74/32.png) [@Krzysztof\_Zarzycki](https://forum.ansible.com/u/Krzysztof_Zarzycki)\
**Post date:** [April 20, 2016, 10:15am UTC](https://forum.ansible.com/t/emulating-host-vars-and-group-vars-for-files/18013/9 "2016-04-20T10:15:09Z")

</div>

Hi! Has anyone found any solution to this problem? Or a good workaround?  
I’m hitting the same issue, that I would like to have host/group or inventory (like dev,prod) specific files, maybe also templates.  
Storing them in variables would not work well, when e.g. you care about indentation, or when the data is binary…

Thanks for any help with that!  
Krzysztof

W dniu niedziela, 19 kwietnia 2015 18:02:22 UTC+2 użytkownik Anand Buddhdev napisał:
