group_vars precedence?

377 views
Skip to first unread message

Duncan Hutty

unread,
Aug 24, 2015, 1:27:34 PM8/24/15
to ansible...@googlegroups.com
I have a host that is a member of 2 groups. Both groups set a variable
'my_var' in `group_vars/<group_name>/`. How do I predict which one wins?
(Ansible-1.9.2)

With this playbook:

---
- hosts: all
gather_facts: no

tasks:
- name: Show groups for this host
debug: var=group_names
- name: Show groups in inventory
debug: var=groups
- debug: var=test_var
- debug: var=second_var


And this inventory, inventory/vagrant_ansible_inventory:

# Generated by Vagrant

vagrant1 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222
ansible_ssh_private_key_file=.vagrant/machines/vagrant1/virtualbox/private_key

[a]
vagrant1

[z]
vagrant1

And these 2 files in group_vars:

inventory/group_vars/a/a.yml
test_var: from_a

inventory/group_vars/z/z.yml:
test_var: from_z


And I run ansible-playbook -i inventory/ playbook.yml, I get

TASK: [debug var=test_var]
****************************************************
ok: [vagrant1] => {
"var": {
"test_var": "from_infra"
}
}

which is exactly as expected for alphabetic precedence (i.e. variables
files from group 'a' get set first and get overridden by variables set
in group 'z').

HOWEVER, if add another inventory group 'infra' to the above inventory
file and another group_vars file, inventory/group_vars/infra/infra.yml,
then the final variable value is from the 'infra' group - it does *NOT*
get overridden the the 'z' group, despite the fact that the ansible run
shows the host is in all 3 groups.

(I've also tried this by setting group membership in the inventory file
and separately with group_by). I can't guess what the rule is for
precedence - it doesn't appear to be alphabetic by name of group or by
name of file.

* Can anyone suggest what the precedence rule is *within* the level of
inventory variables set in group_vars files[1]?

* Or suggest which part of the source I should be looking at to
determine for myself?


[1]:
http://docs.ansible.com/ansible/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable
--
Duncan Hutty
http://www.allgoodbits.org

Bill Wiens

unread,
Jan 19, 2016, 4:44:15 PM1/19/16
to Ansible Project, dhu...@allgoodbits.org
Did you ever find out how this works? I have a variable that's set in both group_vars/aws/secrets.yml and group_vars/aws_dogfood/secrets.yml, and I can't figure out why on some servers it uses the variable from "aws" and others use the variable from "aws_dogfood".

-Bill

Duncan Hutty

unread,
Jan 20, 2016, 9:42:46 AM1/20/16
to ansible...@googlegroups.com, bill.w...@gmail.com


On 1/19/16 16:40, Bill Wiens wrote:
> Did you ever find out how this works? I have a variable that's set in both
> group_vars/aws/secrets.yml and group_vars/aws_dogfood/secrets.yml, and I
> can't figure out why on some servers it uses the variable from "aws" and
> others use the variable from "aws_dogfood".
>
> -Bill

The short answer is 'no', sorry.

But you've brought this back to my mind and hopefully I'll have a chance
to find out. If you do, I'd love to hear it.

I suspect the behaviour is actually 'undefined' because in python, dicts
are not ordered, but I'm not sure. Perhaps we could convince Ansible
that this matters and to replace the relevant dict with an
collections.OrderedDict and insert key/value pairs in a specific
(lexicographic?) order.

In Ansible 2.0+, it's probably somewhere in the VariableManager class
(lib/ansible/vars/__init__.py)
Reply all
Reply to author
Forward
0 new messages