Variable precedence change/fix coming in 2.3

102 views
Skip to first unread message

James Cammarata

unread,
Feb 28, 2017, 11:54:41 PM2/28/17
to ansibl...@googlegroups.com, ansible...@googlegroups.com
Hi all, just wanted to write about a fix/change I'll be merging into the devel branch soon (ie. as soon as the AWS outage quits impacting our testing infrastructure), which means it will be included in the 2.3.0 release.

Per our variable precedence docs (http://docs.ansible.com/ansible/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable), the variables provided by the inventory script or INI file are supposed to be lower priority than those found in group_vars and host_vars files, however this has not been the case in 2.0 and it has gone unnoticed thus far.

While fixing this to match our documentation however, we realized that the precedence docs had a problem - variables defined on the host in the script/INI file would in fact have lower precedence than those in group_vars/all (or any group_vars files). We've decided to fix this based on the following rules for inventory:

1) Host Vars > Group Vars
2) Things relative to the playbook > Things relative to the inventory > Things defined in inventory

For those who are unfamiliar with the second point above, it is possible to have host/group vars files defined relative to both the playbook and to the inventory. For example, if you have:

playbook/site.yml
playbook/group_vars/all
inventory/hosts
inventory/group_vars/all

And you run:

$ ansible-playbook -i inventory/hosts playbook/site.yml

the variables in playbook/group_vars/all will "win" over those in inventory/group_vars/all.

Prior to this change, if you had something like:

ansible_connection: local

In any group_vars/all file, it would have "won" over anything defined in the inventory script, even if you did something like:

myhost ansible_connection=paramiko

the value in the group_vars would have been the value used, which is counter-intuitive.

If anyone has any questions regarding this change, please let us know.

Thanks!

James Cammarata

Ansible Lead/Sr. Principal Software Engineer
Ansible by Red Hat
twitter: @thejimic, github: jimi-c

Brian Coca

unread,
May 22, 2017, 11:26:32 AM5/22/17
to Szabolcs Póta, Ansible Development, Ansible Project
I might have a 'solution' ... configurable precedence, this is
something I've been toying with in
http://github.com/ansible/ansible/issues/23001.

config setting (need to add to examples/ansible.cfg) :
https://github.com/ansible/ansible/pull/23001/files#diff-b77962b6b54a830ec373de0602918318R249

draft implementation:
https://github.com/ansible/ansible/pull/23001/files#diff-473ef6db3f086739e3c053f001d731b5R257


----------
Brian Coca

Brian Coca

unread,
May 23, 2017, 10:15:48 AM5/23/17
to Szabolcs Póta, Ansible Development, Ansible Project
in constants.py:

VARIABLE_PRECEDENCE = get_config(p, DEFAULTS, 'precedence',
'ANSIBLE_PRECEDENCE',
['all_inventory', 'groups_inventory', 'all_plugins_inventory',
'all_plugins_play', 'groups_plugins_inventory',
'groups_plugins_play'], value_type='list')

Which would allow you to switch the group vars precedence, the above
list is the default and matches the current docs.



On Tue, May 23, 2017 at 10:12 AM, Szabolcs Póta <szabol...@gmail.com> wrote:
> Hi Brian,
>
> Thank you for the answer. I have read through your inventory plugin proposal
> JIRA, PR and docs but cannot see where precedence can actually be
> configured. I see that inventory types are now nicely refactored to be
> handled by plugins and that they can be enabled or disabled but can't see
> where precedence is set.
>
> Could you please give me the necessary config option? (The above links do
> redirect me and the pages jump around so probably pasting here directly is
> better). What I would like is to have INI with higher precedence than
> group_vars relative to inventory files.
>
> Thanks,
>
> Szabolcs
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-deve...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
----------
Brian Coca

andrew...@xtra.co.nz

unread,
Aug 9, 2017, 5:33:41 AM8/9/17
to Ansible Project, szabol...@gmail.com, ansibl...@googlegroups.com
Did this make it into 2.3 and if so where would I find constants.py to change the default precedence order?  I would have thought something like this would be set in ansible.cfg

I've been wading through the many posts on how variable precedence works and many including one from you on another post state:

playbook/group_vars are meant to override the inventory as plays are more specific than inventory.

This doesn't make sense to me (and a lot of other people it seems), I had assumed:
  1. A playbook applies a set of roles to one or more servers (i.e. what to do), it would naturally follow that playbooks/group_vars/<group>.yml apply to all servers in the playbook.  
  2. An inventory file defines which servers the playbook will apply the roles to (i.e. where to do it), it should natually follow that we can use inventories/<name>/group_vars/<group>.yml to override playbook group_vars as inventories are more specific about where to run tasks than plays
With the playbook taking precedence over inventories there is no obvious place to have "global" variables for a group (as defined in inventory file) that can be overridden as needed in each inventory. All.yml doesn't make the cut as this can't be used to define group level variables.

So, if this change for changing the default precedence order has not yet made it into Ansible can you point me at any documentation that can show me how to define global variables for groups that can be overridden .  All the links I've followed try to jump though hoops to do what should be a simple excercise and the ansible documentation is rather quiet on the subject (unless I've missed something) - which is a common complaint in the posts I've been reading.

Thanks
  Andrew
Reply all
Reply to author
Forward
0 new messages