# vars in inventory overwrite each other

**URL:** <https://forum.ansible.com/t/vars-in-inventory-overwrite-each-other/15789>\
**Category:** Ansible Project\
**Created:** [July 28, 2014, 11:58am UTC](https://forum.ansible.com/t/vars-in-inventory-overwrite-each-other/15789 "2014-07-28T11:58:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christo\_Buschek](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@Christo\_Buschek](https://forum.ansible.com/u/Christo_Buschek)\
**Post date:** [July 28, 2014, 11:58am UTC](https://forum.ansible.com/t/vars-in-inventory-overwrite-each-other/15789/1 "2014-07-28T11:58:22Z")

</div>

I’m fairly new to Ansible, so maybe what I’m trying to do is bad anyway.

I use ansible to deploy all of our web applications. I use a special deploy playbook for every app. Now every app can be deployed to staging or production. For each of those environments I use a separate inventory such as `envs/staging’. Inside the inventory I use one group for every app and set certain environment specific variables in this way"

`  
[app1]  
[app.example.com](http://app.example.com)  
[app1:vars]  
app-port=3000  
app\_domain=[sub1.example.com](http://sub1.example.com)

[app2]  
[app.example.com](http://app.example.com)  
[app2:vars]  
app-port=3001  
app\_domain=[sub2.example.com](http://sub2.example.com)  
`

I would deploy an app like that: ansible-playbook -i envs/staging deploys/app2.yml -K

Now this doesn’t work unfortunately. In my example it always takes the configuration of app1, even if I deploy app2. In my playbook for app2 I do have the following line: hosts: app2.

So my question would be, why is the playbook for app2 taking configuration options defined for app1? Do I use inventory variables wrong? Do otherpeople use ansible to deploy apps and how do they keep the environments separated?

**UnlockYourBrain GmbH**  
c/o Smart Mobile Factory GmbH

Französische Str. 24

10117 Berlin

Germany

**[www.unlockyourbrain.com](http://www.unlockyourbrain.com/)**

Download at **[Google Play](https://play.google.com/store/apps/details?id=co.unlockyourbrain&referrer=utm_source%3DMA%26utm_medium%3Demail%20footer%26utm_term%3Dgen%26utm_content%3Dgen%26utm_campaign%3Dgen)**

Like us on **Facebook**

Geschäftsführer: Felix Nienstädt, Simon Smend

Unternehmenssitz: Berlin

Eingetragen am Handelsgericht Charlottenburg unter HRB 149905 B

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 28, 2014, 2:11pm UTC](https://forum.ansible.com/t/vars-in-inventory-overwrite-each-other/15789/2 "2014-07-28T14:11:53Z")

</div>

Groups at the same level of depth will definitely override each other.

What you need to do is consider using role variables, or vars\_files, or “vars”, etc, or name the variable something like “app2\_port”

There are some good tips on [http://docs.ansible.com/playbooks\_variables.html](http://docs.ansible.com/playbooks_variables.html)

---

<div class="post-metadata">

**Author:** ![Christo\_Buschek](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@Christo\_Buschek](https://forum.ansible.com/u/Christo_Buschek)\
**Post date:** [July 29, 2014, 1:04pm UTC](https://forum.ansible.com/t/vars-in-inventory-overwrite-each-other/15789/3 "2014-07-29T13:04:02Z")

</div>

Okay, thanks for this info. I worked around it by creating a group\_var file for each environment and import it into the playbook using vars\_file. It works good enough for me.

> I’m fairly new to Ansible, so maybe what I’m trying to do is bad anyway.
> 
> I use ansible to deploy all of our web applications. I use a special deploy playbook for every app. Now every app can be deployed to staging or production. For each of those environments I use a separate inventory such as `envs/staging’. Inside the inventory I use one group for every app and set certain environment specific variables in this way"
> 
> `  
> [app1]  
> [app.example.com](http://app.example.com)  
> [app1:vars]  
> app-port=3000  
> app\_domain=[sub1.example.com](http://sub1.example.com)
> 
> [app2]  
> [app.example.com](http://app.example.com)  
> [app2:vars]  
> app-port=3001  
> app\_domain=[sub2.example.com](http://sub2.example.com)  
> `
> 
> I would deploy an app like that: ansible-playbook -i envs/staging deploys/app2.yml -K
> 
> Now this doesn’t work unfortunately. In my example it always takes the configuration of app1, even if I deploy app2. In my playbook for app2 I do have the following line: hosts: app2.
> 
> So my question would be, why is the playbook for app2 taking configuration options defined for app1? Do I use inventory variables wrong? Do otherpeople use ansible to deploy apps and how do they keep the environments separated?
> 
> **UnlockYourBrain GmbH**  
> c/o Smart Mobile Factory GmbH
> 
> Französische Str. 24
> 
> 10117 Berlin
> 
> Germany
> 
> **[www.unlockyourbrain.com](http://www.unlockyourbrain.com/)**
> 
> Download at **[Google Play](https://play.google.com/store/apps/details?id=co.unlockyourbrain&referrer=utm_source%3DMA%26utm_medium%3Demail%20footer%26utm_term%3Dgen%26utm_content%3Dgen%26utm_campaign%3Dgen)**
> 
> Like us on **Facebook**
> 
> Geschäftsführer: Felix Nienstädt, Simon Smend
> 
> Unternehmenssitz: Berlin
> 
> Eingetragen am Handelsgericht Charlottenburg unter HRB 149905 B

**UnlockYourBrain GmbH**  
c/o Smart Mobile Factory GmbH

Französische Str. 24

10117 Berlin

Germany

**[www.unlockyourbrain.com](http://www.unlockyourbrain.com/)**

Download at **[Google Play](https://play.google.com/store/apps/details?id=co.unlockyourbrain&referrer=utm_source%3DMA%26utm_medium%3Demail%20footer%26utm_term%3Dgen%26utm_content%3Dgen%26utm_campaign%3Dgen)**

Like us on **Facebook**

Geschäftsführer: Felix Nienstädt, Simon Smend

Unternehmenssitz: Berlin

Eingetragen am Handelsgericht Charlottenburg unter HRB 149905 B

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 29, 2014, 1:33pm UTC](https://forum.ansible.com/t/vars-in-inventory-overwrite-each-other/15789/4 "2014-07-29T13:33:37Z")

</div>

Definitely do not import things out of “group\_vars” with the vars\_files directive, it would be ok to keep it in a different directory, but you are going to find this would confuse most ansible users using the directory for two purposes.

---

<div class="post-metadata">

**Author:** ![Christo\_Buschek](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@Christo\_Buschek](https://forum.ansible.com/u/Christo_Buschek)\
**Post date:** [July 29, 2014, 1:50pm UTC](https://forum.ansible.com/t/vars-in-inventory-overwrite-each-other/15789/5 "2014-07-29T13:50:14Z")

</div>

yeah, good point. Thanks.
