# Ansible Best Practices - Group\_Vars Not Being Applied As Expected

**URL:** <https://forum.ansible.com/t/ansible-best-practices-group-vars-not-being-applied-as-expected/31486>\
**Category:** Ansible Project\
**Created:** [October 18, 2019, 6:35am UTC](https://forum.ansible.com/t/ansible-best-practices-group-vars-not-being-applied-as-expected/31486 "2019-10-18T06:35:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Baldwin](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@Jeff\_Baldwin](https://forum.ansible.com/u/Jeff_Baldwin)\
**Post date:** [October 18, 2019, 6:35am UTC](https://forum.ansible.com/t/ansible-best-practices-group-vars-not-being-applied-as-expected/31486/1 "2019-10-18T06:35:30Z")

</div>

All,

I have been setting up my environment to match the best practices provided here: [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_best\_practices.html#best-practices](https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#best-practices) – However, I am running into some issues with how variables are being

**Goal:**

- The system administrator user/group defined in group\_vars/all.yml will be created on BOTH hosts
- The oracle dba user/group defined in group\_vars/oracle.yml will be created on the oracle box ONLY

**What I Am Seeing:**

- The ‘jeff5’ user (system adminstrator) and AIXAdmin group are being created on the lx00011010d box ONLY (the expectation is that it would be created on both)
- The oracle/DBA users are being created on lx00016141d, which is NOT the oracle box (expectation is that these would get created on lx00011010d only)

Code and output below:

**[jeff@lx00010300p ansible-help]$ tree**

.

> – ansible.cfg  
> – common.yml  
> – group\_vars
> 
> > – all.yml  
> > `-- oracle.yml -- inventory -- oracle `– other  
> > – oracle.yml  
> > – roles  
> > `-- ansible-users -- defaults `– main.yml  
> > – README.md  
> > `-- tasks `– main.yml  
> > `-- site.yml

[jeff@lx00010300p ansible-help]$ cat site.yml common.yml oracle.yml

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 18, 2019, 9:58am UTC](https://forum.ansible.com/t/ansible-best-practices-group-vars-not-being-applied-as-expected/31486/2 "2019-10-18T09:58:01Z")

</div>

It's not possible to explain the output below and other results you observe  
with the input provided. You'll have to minimise the example and make it  
verifiable. It's simply out of scope this way.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PLAY [oracle]

---

<div class="post-metadata">

**Author:** ![Jeff\_Baldwin](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@Jeff\_Baldwin](https://forum.ansible.com/u/Jeff_Baldwin)\
**Post date:** [October 18, 2019, 2:11pm UTC](https://forum.ansible.com/t/ansible-best-practices-group-vars-not-being-applied-as-expected/31486/3 "2019-10-18T14:11:49Z")

</div>

Thank you for your time vlado. Below is the information you have requested:

**Command Line:**  
ansible-playbook site.yml -C

**ansible-inventory --list --vars**  
**Question:** Why is group ‘AIXAdmin’ and user ‘jeff5’ vars not getting applied to both boxes?

[admin@lx00010300p ansible-help]$ ansible-inventory --list --vars  
{  
“\_meta”: {  
“hostvars”: {  
“lx00011010d”: {  
“groups\_to\_create”: [  
{  
“gid”: “901”,  
“name”: “AIXAdmin”,  
“state”: “present”  
}  
],  
“users”: [  
{  
“group”: “AIXAdmin”,  
“name”: “Jeff - AIX Linux Engineer”,  
“uid”: 318427,  
“username”: “jeff5”  
}  
]  
},  
“lx00016141d”: {  
“file”: “group\_vars/oracle.yml”,  
“groups\_to\_create”: [  
{  
“gid”: 201,  
“name”: “dba”  
}  
],  
“users”: [  
{  
“group”: “dba”,  
“name”: “Oracle”,  
“uid”: 202,  
“username”: “oracle”  
},  
{  
“group”: “dba”,  
“name”: “Kathryn - DBA”,  
“uid”: 225114,  
“username”: “kwhit”  
}  
]  
}  
}  
},  
“all”: {  
“children”: [  
“oracle”,  
“other”,  
“ungrouped”  
]  
},  
“oracle”: {  
“hosts”: [  
“lx00016141d”  
]  
},  
“other”: {  
“hosts”: [  
“lx00011010d”  
]  
}  
}

**Ansible-Users role details:**  
There are only two files in this role. Details below:

**[admin@lx00010300p ansible-users]$ cat defaults/main.yml**

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 19, 2019, 1:47pm UTC](https://forum.ansible.com/t/ansible-best-practices-group-vars-not-being-applied-as-expected/31486/4 "2019-10-19T13:47:34Z")

</div>

> \> \> ################################### INVENTORY  
> \> \> # file: \*inventory/oracle\*  
> \> \> [oracle]  
> \> \> lx00011010d  
> \> \>  
> \> \> # file: \*inventory/other\*  
> \> \> [other]  
> \> \> lx00016141d  
> \> \> [...]  
> \> \> ################################### GROUP\_VARS

> \*ansible-inventory --list --vars\*  
> [...]  
> &nbsp;&nbsp;&nbsp;&nbsp;"oracle": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"hosts": [  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"lx00016141d"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
> &nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;"other": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"hosts": [  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"lx00011010d"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> }

> [admin@lx00010300p ansible-help]$ ansible-config dump  
> [...]  
> DEFAULT\_HOST\_LIST(/sdd/ansible-help/ansible.cfg) =  
> [u'/sdd/ansible-production/inventory']

The problem might be DEFAULT\_HOST\_LIST. The doc says  
DEFAULT\_HOST\_LIST: "Comma separated list of Ansible inventory sources"  
[https://docs.ansible.com/ansible/latest/reference\_appendices/config.html#default-host-list](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-host-list)

Fix the configuration. For example in ansible.cfg

&nbsp;&nbsp;&nbsp;&nbsp;[defaults]  
&nbsp;&nbsp;&nbsp;&nbsp;inventory = /sdd/ansible-production/inventory/oracle,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/sdd/ansible-production/inventory/other

(note: ini\_file doesn't handle multi-line entries #4413)  
[https://github.com/ansible/ansible-modules-core/issues/4413](https://github.com/ansible/ansible-modules-core/issues/4413)

If this does not help proceed with the simplified test below.

I had set this option also to \*directory\* "inventory", but this does not seem  
to be a problem and I was not able to reproduce the behaviour where the groups  
"oracle" and "other" swap the hosts.

&nbsp;&nbsp;&nbsp;&nbsp;$ grep inventory ansible.cfg  
&nbsp;&nbsp;&nbsp;&nbsp;inventory = $PWD/inventory

&nbsp;&nbsp;&nbsp;&nbsp;$ tree inventory/  
&nbsp;&nbsp;&nbsp;&nbsp;inventory/  
&nbsp;&nbsp;&nbsp;&nbsp;├── oracle  
&nbsp;&nbsp;&nbsp;&nbsp;└── other

&nbsp;&nbsp;&nbsp;&nbsp;$ cat inventory/oracle  
&nbsp;&nbsp;&nbsp;&nbsp;[oracle]  
&nbsp;&nbsp;&nbsp;&nbsp;lx00011010d

&nbsp;&nbsp;&nbsp;&nbsp;$ cat inventory/other  
&nbsp;&nbsp;&nbsp;&nbsp;[other]  
&nbsp;&nbsp;&nbsp;&nbsp;lx00016141d

&nbsp;&nbsp;&nbsp;&nbsp;$ tree group\_vars  
&nbsp;&nbsp;&nbsp;&nbsp;group\_vars  
&nbsp;&nbsp;&nbsp;&nbsp;├── all.yml  
&nbsp;&nbsp;&nbsp;&nbsp;└── oracle.yml

&nbsp;&nbsp;&nbsp;&nbsp;$ cat group\_vars/all.yml  
&nbsp;&nbsp;&nbsp;&nbsp;users:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- username: jeff5

&nbsp;&nbsp;&nbsp;&nbsp;$ cat group\_vars/oracle.yml  
&nbsp;&nbsp;&nbsp;&nbsp;users:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- username: oracle

&nbsp;&nbsp;&nbsp;&nbsp;$ ansible-inventory --list --var  
&nbsp;&nbsp;&nbsp;&nbsp;[...]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"oracle": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"hosts": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"lx00011010d"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"other": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"hosts": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"lx00016141d"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

The goal is to explain why you see the groups "oracle" and "other" swapping  
the hosts. Try to reproduce the simplified test above, step by step increase  
the complexity of the use-case and isolate the problem.

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Jeff\_Baldwin](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@Jeff\_Baldwin](https://forum.ansible.com/u/Jeff_Baldwin)\
**Post date:** [October 25, 2019, 7:15am UTC](https://forum.ansible.com/t/ansible-best-practices-group-vars-not-being-applied-as-expected/31486/5 "2019-10-25T07:15:45Z")

</div>

Hi Vladimir,

Thank you again for your time.

[https://justpaste.it/4jmp1](https://justpaste.it/4jmp1) ← I have put my steps/output here in case it is easier to view.

Also, I simplified my example a bit. I am still unclear on why ‘jeff’ is not getting added to both servers:

**[jeff@lx00010300p ansible-production3]$ ansible -i hosts -m ping all**  
lx00011010d | SUCCESS =\> {  
“ansible\_facts”: {  
“discovered\_interpreter\_python”: “/usr/bin/python”  
},  
“changed”: false,  
“ping”: “pong”  
}  
lx00016141d | SUCCESS =\> {  
“ansible\_facts”: {  
“discovered\_interpreter\_python”: “/usr/bin/python”  
},  
“changed”: false,  
“ping”: “pong”  
}  
**[jeff@lx00010300p ansible-production3]$ ansible -i hosts -m ping oracle**  
lx00011010d | SUCCESS =\> {  
“ansible\_facts”: {  
“discovered\_interpreter\_python”: “/usr/bin/python”  
},  
“changed”: false,  
“ping”: “pong”  
}

**[ifreling@lx00010300p ansible-production3]$ cat group\_vars/oracle**

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 25, 2019, 9:35am UTC](https://forum.ansible.com/t/ansible-best-practices-group-vars-not-being-applied-as-expected/31486/6 "2019-10-25T09:35:32Z")

</div>

Short story is that variables from "group\_vars/all" are overridden by  
variables from "group\_vars/oracle".

See \*Variable precedence: Where should I put a variable?\*  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#variable-precedence-where-should-i-put-a-variable](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable)

It's possible to configure "hash\_behaviour = merge", but quoting from the  
doc:" This is not the default behavior and it does not affect variables whose  
values are scalars (integers, strings) or arrays. We generally recommend not  
using this setting unless you think you have an absolute need for it, ..."  
[https://docs.ansible.com/ansible/latest/reference\_appendices/config.html#default-hash-behaviour](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-hash-behaviour)

There are other options how to merge dictionaries. For example  
\*How to extend dict in ansible, not overwriting it?\*  
[https://stackoverflow.com/questions/57335762/how-to-extend-dict-in-ansible-not-overwriting-it/](https://stackoverflow.com/questions/57335762/how-to-extend-dict-in-ansible-not-overwriting-it/)

Details

---

<div class="post-metadata">

**Author:** ![acozine1](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@acozine1](https://forum.ansible.com/u/acozine1)\
**Post date:** [October 25, 2019, 2:58pm UTC](https://forum.ansible.com/t/ansible-best-practices-group-vars-not-being-applied-as-expected/31486/7 "2019-10-25T14:58:05Z")

</div>

For anyone looking to control the priority of variables at the same parent/child level of group variables, there’s also the `ansible_group_priority` setting. See [https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html#how-variables-are-merged](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#how-variables-are-merged).

---

<div class="post-metadata">

**Author:** ![Jeff\_Baldwin](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@Jeff\_Baldwin](https://forum.ansible.com/u/Jeff_Baldwin)\
**Post date:** [October 25, 2019, 3:29pm UTC](https://forum.ansible.com/t/ansible-best-practices-group-vars-not-being-applied-as-expected/31486/8 "2019-10-25T15:29:10Z")

</div>

Thank you alicia.

Given my examples, would you be able to advise on how I could apply this here to accomplish my goal?

Thanks.
