# some questions related to vars\_plugins

**URL:** <https://forum.ansible.com/t/some-questions-related-to-vars-plugins/12623>\
**Category:** Ansible Project\
**Created:** [April 3, 2013, 8:52pm UTC](https://forum.ansible.com/t/some-questions-related-to-vars-plugins/12623 "2013-04-03T20:52:52Z")\
**Posts on this page:** 3\
**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 3, 2013, 8:52pm UTC](https://forum.ansible.com/t/some-questions-related-to-vars-plugins/12623/1 "2013-04-03T20:52:52Z")

</div>

Hi,

I have been reading some ansible code, and am trying to understand some inner workings.  
I have some questions, to check if I understand certain things correctly:

1 Am i correct when stating that the the result of inventory scripts can be complemented with host/group\_vars?

2 If (1), which variables get precedence? If I read it right, vars returned by the inventory script will prevail?

3 Is there a good reason why we have the two separate dirs host\_vars/ and group\_vars/? It seems to me we could easily merge them in, say, one vars/ dir?

4 It seems to me VarsModule.run() in group\_vars.py will loop through every group/host, and check if a  
file with that respective name exists, even if the group\_vars and/or host\_vars dir don’t exist? If so, checking if those dirs exist before looping seems more efficient to me?

5 I’m considering writing a vars\_plugin, very specific for our environment, to calculate specific ip’s based on a configured vlan defined per server group; for this I need a config file where I can define this vlan number variable, and which my script will parse. What would be a proper location for this file?

Thx for your thought!

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 3, 2013, 9:15pm UTC](https://forum.ansible.com/t/some-questions-related-to-vars-plugins/12623/2 "2013-04-03T21:15:56Z")

</div>

> Hi,
> 
> I have been reading some ansible code, and am trying to understand some  
> inner workings.  
> I have some questions, to check if I understand certain things correctly:
> 
> 1 Am i correct when stating that the the result of inventory scripts can be  
> complemented with host/group\_vars?

Yes! host\_vars/ and group\_vars/ are always used.

> 2 If (1), which variables get precedence? If I read it right, vars returned  
> by the inventory script will prevail?

Variables in group\_vars/host\_vars are considered to be inventory  
variables, thus precedence between the inventory script and  
them are officially undefined, but your reading of the code is correct.

The recommended way to set defaults remains as group\_vars/all OR  
setting variables in the 'all' group via the inventory script.

> 3 Is there a good reason why we have the two separate dirs host\_vars/ and  
> group\_vars/? It seems to me we could easily merge them in, say, one vars/  
> dir?

Ansible does not require that group names and host names be unique.

Further, it is much nicer to have them organized seperately and not  
have them intermixed when finding what you want.

Basically I want to organize my group variables and host variables  
seperately, and this also makes sure I can look to my host\_vars to see  
that it is minimally used --  
host based configuration can be a neccessary evil, but it is desirable  
to remove as much of the "unique snowflake" configuration as possible  
and push more configuration  
towards group variables.

> 4 It seems to me VarsModule.run() in group\_vars.py will loop through every  
> group/host, and check if a  
> file with that respective name exists, even if the group\_vars and/or  
> host\_vars dir don't exist? If so, checking if those dirs exist before  
> looping seems more efficient to me?

Sure. I bet in the grand scheme of things it doesn't add up, but  
patches to add an extra check would be accepted.

> 5 I'm considering writing a vars\_plugin, very specific for our environment,  
> to calculate specific ip's based on a configured vlan defined per server  
> group; for this I need a config file where I can define this vlan number  
> variable, and which my script will parse. What would be a proper location  
> for this file?
> 
> Thx for your thought!

There are several places you can configure plugins.

One is the plugin path in the config file, another would be a  
./vars\_plugins/ directory relative to your playbooks.

Ansible will load files from there automatically.

You can also set this path with an environment variable.

Since you're digging in the code, I should point out this path is  
configured here:

[https://github.com/ansible/ansible/blob/devel/lib/ansible/constants.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/constants.py)

DEFAULT\_VARS\_PLUGIN\_PATH= shell\_expand\_path(get\_config(p, DEFAULTS,  
'vars\_plugins', 'ANSIBLE\_VARS\_PLUGINS',  
'/usr/share/ansible\_plugins/vars\_plugins'))

which implies the environment variable ANSIBLE\_VARS\_PLUGINS can also  
be set to control this location.

---

<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 3, 2013, 9:27pm UTC](https://forum.ansible.com/t/some-questions-related-to-vars-plugins/12623/3 "2013-04-03T21:27:00Z")

</div>

​Thanks or your quick reply.  
​
