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