Skip to content

Instantly share code, notes, and snippets.

@nrb
Last active December 16, 2016 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nrb/179053241c3035f57bd9a8d89b0f8410 to your computer and use it in GitHub Desktop.
Save nrb/179053241c3035f57bd9a8d89b0f8410 to your computer and use it in GitHub Desktop.
Testing group_var resolution with various inventory configurations
The more specific group var values win; there isn't any bleed over from parent or sibling groups in these tests.

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

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]
groupC

[groupA:vars]
test_var=A

[groupB]
two ansible_host=10.0.3.31 ansible_ssh_pass=secrete

[groupB:children]
groupC

[groupB:vars]
test_var=B

[groupC]
three ansible_host=10.0.3.205 ansible_ssh_pass=secrete

[groupC:vars]
test_var=C

Targetting all:

root@nrb-ord-aio:~# ansible-playbook -i hosts debug-playbook.yml

PLAY [all] *********************************************************************

TASK [View test_var value] *****************************************************
ok: [three] => {
    "msg": "System three has value C"
}
ok: [one] => {
    "msg": "System one has value A"
}
ok: [two] => {
    "msg": "System two has value B"
}

TASK [View file_var value] *****************************************************
ok: [three] => {
    "msg": "System three has file value In groupC"
}
ok: [one] => {
    "msg": "System one has file value In groupA"
}
ok: [two] => {
    "msg": "System two has file value In groupB"
}

PLAY RECAP *********************************************************************
one                        : ok=2    changed=0    unreachable=0    failed=0
three                      : ok=2    changed=0    unreachable=0    failed=0
two                        : ok=2    changed=0    unreachable=0    failed=0

Targetting groupA:

root@nrb-ord-aio:~# ansible-playbook -i hosts debug-playbook.yml

PLAY [groupA] ******************************************************************

TASK [View test_var value] *****************************************************
ok: [one] => {
    "msg": "System one has value A"
}
ok: [three] => {
    "msg": "System three has value C"
}

TASK [View file_var value] *****************************************************
ok: [three] => {
    "msg": "System three has file value In groupC"
}
ok: [one] => {
    "msg": "System one has file value In groupA"
}

PLAY RECAP *********************************************************************
one                        : ok=2    changed=0    unreachable=0    failed=0
three                      : ok=2    changed=0    unreachable=0    failed=0

Targetting groupB:

root@nrb-ord-aio:~# ansible-playbook -i hosts debug-playbook.yml

PLAY [groupB] ******************************************************************

TASK [View test_var value] *****************************************************
ok: [three] => {
    "msg": "System three has value C"
}
ok: [two] => {
    "msg": "System two has value B"
}

TASK [View file_var value] *****************************************************
ok: [three] => {
    "msg": "System three has file value In groupC"
}
ok: [two] => {
    "msg": "System two has file value In groupB"
}

PLAY RECAP *********************************************************************
three                      : ok=2    changed=0    unreachable=0    failed=0
two                        : ok=2    changed=0    unreachable=0    failed=0

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"
}

TASK [View file_var value] *****************************************************
ok: [three] => {
    "msg": "System three has file value In groupC"
}

PLAY RECAP *********************************************************************
three                      : ok=2    changed=0    unreachable=0    failed=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment