Expectation:
one -> groupA only two -> groupB when targetted three -> groupC when targetted
Ansible used:
root@nrb-ord-aio:~# ansible --version
ansible 2.2.0.0
config file =
configured module search path = Default w/o overrides
Inventory configuration:
root@nrb-ord-aio:~# cat hosts
[groupA]
one ansible_host=10.0.3.141 ansible_ssh_pass=secrete
[groupA:children]
groupB
groupC
[groupA:vars]
test_var=A
[groupB]
two ansible_host=10.0.3.31 ansible_ssh_pass=secrete
[groupB:vars]
test_var=B
[groupC]
three ansible_host=10.0.3.205 ansible_ssh_pass=secrete
[groupC:vars]
test_var=C
Pinging to confirm membership is as we expect:
root@nrb-ord-aio:~# ansible -i hosts -m ping groupA
three | SUCCESS => {
"changed": false,
"ping": "pong"
}
two | SUCCESS => {
"changed": false,
"ping": "pong"
}
one | SUCCESS => {
"changed": false,
"ping": "pong"
}
root@nrb-ord-aio:~# ansible -i hosts -m ping groupB
two | SUCCESS => {
"changed": false,
"ping": "pong"
}
root@nrb-ord-aio:~# ansible -i hosts -m ping groupC
three | SUCCESS => {
"changed": false,
"ping": "pong"
}
What happens to test_var
's value when targetting different groups?
The test playbook:
root@nrb-ord-aio:~# cat debug-playbook.yml
---
- hosts: all
gather_facts: false
tasks:
- name: View test_var value
debug:
msg: "System {{ inventory_hostname }} has value {{ test_var }}"
The output from targetting all:
root@nrb-ord-aio:~# ansible-playbook -i hosts debug-playbook.yml
PLAY [all] *********************************************************************
TASK [View test_var value] *****************************************************
ok: [two] => {
"msg": "System two has value B"
}
ok: [three] => {
"msg": "System three has value C"
}
ok: [one] => {
"msg": "System one has value A"
}
PLAY RECAP *********************************************************************
one : ok=1 changed=0 unreachable=0 failed=0
three : ok=1 changed=0 unreachable=0 failed=0
two : ok=1 changed=0 unreachable=0 failed=0
From targetting groupA:
root@nrb-ord-aio:~# ansible-playbook -i hosts debug-playbook.yml
PLAY [groupA] ******************************************************************
TASK [View test_var value] *****************************************************
ok: [three] => {
"msg": "System three has value C"
}
ok: [two] => {
"msg": "System two has value B"
}
ok: [one] => {
"msg": "System one has value A"
}
PLAY RECAP *********************************************************************
one : ok=1 changed=0 unreachable=0 failed=0
three : ok=1 changed=0 unreachable=0 failed=0
two : ok=1 changed=0 unreachable=0 failed=0
From targetting groupB:
root@nrb-ord-aio:~# ansible-playbook -i hosts debug-playbook.yml
PLAY [groupB] ******************************************************************
TASK [View test_var value] *****************************************************
ok: [two] => {
"msg": "System two has value B"
}
PLAY RECAP *********************************************************************
two : ok=1 changed=0 unreachable=0 failed=0
From targetting groupC:
root@nrb-ord-aio:~# ansible-playbook -i hosts debug-playbook.yml
PLAY [groupC] ******************************************************************
TASK [View test_var value] *****************************************************
ok: [three] => {
"msg": "System three has value C"
}
PLAY RECAP *********************************************************************
three : ok=1 changed=0 unreachable=0 failed=0