some questions related to vars_plugins

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

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

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.

​Thanks or your quick reply.