# inventory file
host1
host2
[group1]
host1
host2
[group11:children]
group1
[group12:children]
group1# content of group_vars/group11
var: "group11"
# content of group_vars/group12
var: "group12"
#content of playbook site-group11.yml
- hosts: group11
tasks:
- debug: var=var
#content of playbook site-group12.yml
- hosts: group12
tasks:
- debug: var=var< TASK: debug var=var >
---------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
ok: [host1] => {
"var": {
"var": "group12"
}
}
ok: [host2] => {
"var": {
"var": "group12"
}
}
site-group12.yml. But my expectation is that the variable var has the value group11 in playbook site-group11.yml .
So my question, why my expectation is wrong?
Best regards,So my question, why my expectation is wrong?
This is a feature and not a bug, and has been asked several times yet here and on the list. You are trying to use a way to define things that just isn't the way ansible dpes things.
Ansible only uses variables on the host level. The inventory only returns variables after they are calculated/inherited down top that host level.
When you target a play to a particular group, that gets calculated
down to a list of hosts. Whether you call groupA or groupB here, it
results in the same host. And that hosts only has one particular value
for myvar, which happens to be groupB because thats the last one bing parsed.
To quote the docs:
Remember: Child groups override parent groups, and hosts always override their groups.