# dynamic inventory, group vars and role vars precedence

**URL:** <https://forum.ansible.com/t/dynamic-inventory-group-vars-and-role-vars-precedence/16373>\
**Category:** Ansible Developer\
**Created:** [September 29, 2014, 7:03pm UTC](https://forum.ansible.com/t/dynamic-inventory-group-vars-and-role-vars-precedence/16373 "2014-09-29T19:03:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathieu\_Guillaume](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Mathieu\_Guillaume](https://forum.ansible.com/u/Mathieu_Guillaume)\
**Post date:** [September 29, 2014, 7:03pm UTC](https://forum.ansible.com/t/dynamic-inventory-group-vars-and-role-vars-precedence/16373/1 "2014-09-29T19:03:55Z")

</div>

Hello Ansiblers,

This is about two different things (I know, bad!), but all about variables. This is on Ansible 1.7.2.  
I’ve put up my test code on [https://github.com/mguillaume/ansible-vars-test](https://github.com/mguillaume/ansible-vars-test) for easy review/reproducing.

First, unless I messed up somewhere, it seems hosts dynamically added to a group still don’t pick up the correct group\_vars (supposed to have been fixed for [https://github.com/ansible/ansible/issues/8605](https://github.com/ansible/ansible/issues/8605) )  
 → See test 1&2

Second, it seems role variables take precedence over group variables. Is that normal? I would have expected the opposite myself.  
 → See test 3

Side note 1:  
I’m using localhost for my host in the tests, could be there’s some specific code for it that makes things break.

Side note 2:  
If I have localhost in group test in my hosts-static inventory file and run with ansible-playbook -i hosts-static,localhost , I end up with localhost not in group test.  
Since I’ve seen elsewhere that running things this way was kinda hackish and not supported this may just be something that doesn’t need to be fixed.

Any insight welcome 🙂

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [September 29, 2014, 7:16pm UTC](https://forum.ansible.com/t/dynamic-inventory-group-vars-and-role-vars-precedence/16373/2 "2014-09-29T19:16:37Z")

</div>

> First, unless I messed up somewhere, it seems hosts dynamically added to a  
> group still don't pick up the correct group\_vars (supposed to have been  
> fixed for [https://github.com/ansible/ansible/issues/8605](https://github.com/ansible/ansible/issues/8605) )  
> -\> See test 1&2

​There have been so​me patches in that area. Could you check if that is  
still a rpoblem in 1.8 devel?

> Second, it seems role variables take precedence over group variables. Is  
> that normal? I would have expected the opposite myself.  
> -\> See test 3

​role/vars​

​take precedence over inventory vars yes.​ to avoid that, consider using  
role/defaults

> Side note 1:  
> I'm using localhost for my host in the tests, could be there's some  
> specific code for it that makes things break.

​localhot is something special, so yes, that is possible; try testing with  
a "remote host" (which you can access -c local)​

​but test avoiding using the name localhost or the loopback ip ​

> Side note 2:  
> If I have localhost in group test in my hosts-static inventory file and  
> run with ansible-playbook -i hosts-static,localhost , I end up with  
> localhost not in group test.  
> Since I've seen elsewhere that running things this way was kinda hackish  
> and not supported this may just be something that doesn't need to be fixed.

​Yes. Hackish things are special 🙂

---

<div class="post-metadata">

**Author:** ![Mathieu\_Guillaume](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Mathieu\_Guillaume](https://forum.ansible.com/u/Mathieu_Guillaume)\
**Post date:** [September 29, 2014, 7:43pm UTC](https://forum.ansible.com/t/dynamic-inventory-group-vars-and-role-vars-precedence/16373/3 "2014-09-29T19:43:57Z")

</div>

Working good in 1.8 devel.
