# Misunderstanding about host + var + group\_vars

**URL:** <https://forum.ansible.com/t/misunderstanding-about-host-var-group-vars/24200>\
**Category:** Ansible Project\
**Created:** [April 12, 2017, 1:50pm UTC](https://forum.ansible.com/t/misunderstanding-about-host-var-group-vars/24200 "2017-04-12T13:50:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex6](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@Alex6](https://forum.ansible.com/u/Alex6)\
**Post date:** [April 12, 2017, 1:50pm UTC](https://forum.ansible.com/t/misunderstanding-about-host-var-group-vars/24200/1 "2017-04-12T13:50:08Z")

</div>

I’ve seen others post similar experiences, but I can never quite get my head around it. If someone could explain the following behaviour that would be great.

So i have mydev.yml and mylive.yml

They look something like:

mydev.yml

`

- hosts: dev\_place  
sudo: no

`

mylive.yml

`

- hosts: live\_place  
sudo: no

`

hosts

`  
[dev\_place]  
10.10.10.10

[dev\_place:vars]  
ansible\_private\_key\_file=/home/dev\_user/.ssh/key

[live\_place]  
10.10.10.10

[live\_place:vars]  
ansible\_private\_key\_file=/home/live\_user/.ssh/key

`

Both hosts point to the same server but reference different paths for their keys. This is because I can run locally on my vagrant or run it on a live server - both need to talk to the same server.

So I run this locally:

`ansible-playbook mydev.yml-i hosts -vvv`

From the printout, I can see it is using live\_place vars

I’ve tried removing the :vars from here, and adding:

`  
group\_vars.dev\_place.yml  
group\_vars.live\_place.yml

`

So then these would have the relative variables, but again it would be using which ever hosts are listed last.

So if I swap dev\_place and live\_place around in the hosts file, it will then take dev\_place vars.

I know ansible has a specific way of how it deals with vars, but I’m really struggling to understand.  
What’s the point of passing a config name if it’s just going to grab the last vars from somewhere else…?

I apologise for my misunderstanding on this stuff.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 12, 2017, 9:39pm UTC](https://forum.ansible.com/t/misunderstanding-about-host-var-group-vars/24200/2 "2017-04-12T21:39:25Z")

</div>

You are not passing a config name, you are passing a group name,  
groups in Ansible are used for 2 things:

- a way to target hosts  
- a way to bulk assign variables to a host

As such variables are not assigned to the group when you target it in  
the play, only the host, since the same host is member of 2 groups it  
inherits the variables from both, not just the one you target in the  
play. Since there is a conflict the 'last one merged wins', groups are  
evaluated by child/parent relationship and then by name. (in 2.4 we  
introduce priorities to give you fine grained control ... docs  
pending).

For your situation, I would either use 2 different inventories, (live  
and dev) or key that variable via extra vars or vars\_files.
