# Inventory file - different groups with same  variables but not the same  value

**URL:** <https://forum.ansible.com/t/inventory-file-different-groups-with-same-variables-but-not-the-same-value/15289>\
**Category:** Ansible Project\
**Created:** [May 21, 2014, 11:28am UTC](https://forum.ansible.com/t/inventory-file-different-groups-with-same-variables-but-not-the-same-value/15289 "2014-05-21T11:28:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sacha\_Vukovic](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@Sacha\_Vukovic](https://forum.ansible.com/u/Sacha_Vukovic)\
**Post date:** [May 21, 2014, 11:28am UTC](https://forum.ansible.com/t/inventory-file-different-groups-with-same-variables-but-not-the-same-value/15289/1 "2014-05-21T11:28:47Z")

</div>

Hello,

after reading this post :  
[https://groups.google.com/forum/#!topic/ansible-project/XbVNoUED2cU](https://groups.google.com/forum/#!topic/ansible-project/XbVNoUED2cU)  
[http://docs.ansible.com/intro\_inventory.html](http://docs.ansible.com/intro_inventory.html)

I have a similar question with inventory file,

I want to provisionning on the same host two tomcat using same role, but http port, path must be different.

Example:

In my inventory file prod.hosts  
[tomcat1]  
host1

[tomcat2]  
host1

[tomcat1:vars]  
product\_dir=‘/home/produit/tomcat1’  
[tomcat2:vars]  
product\_dir=‘/home/produit/tomcat2’

In site.yml

- hosts: tomcat1  
user: root  
pre\_tasks:

- debug: msg={{product\_dir}}

- hosts: tomcat2  
user: root  
pre\_tasks:

- debug: msg={{product\_dir}}

playbook:

PLAY [tomcat1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1]

TASK: [debug msg={{product\_dir}}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [srv-vid-tux48] =\> {  
“msg”: “/data/produits/tomcat1”  
}

PLAY [tomcat2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [srv-vid-tux48]

TASK: [debug msg={{product\_dir}}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [srv-vid-tux48] =\> {  
“msg”: “/data/produits/tomcat1”  
}

ansible version 1.6.1

---

<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:** [May 21, 2014, 10:13pm UTC](https://forum.ansible.com/t/inventory-file-different-groups-with-same-variables-but-not-the-same-value/15289/2 "2014-05-21T22:13:54Z")

</div>

What’s your question?

Thanks.

---

<div class="post-metadata">

**Author:** ![Sacha\_Vukovic](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@Sacha\_Vukovic](https://forum.ansible.com/u/Sacha_Vukovic)\
**Post date:** [May 22, 2014, 8:21am UTC](https://forum.ansible.com/t/inventory-file-different-groups-with-same-variables-but-not-the-same-value/15289/3 "2014-05-22T08:21:33Z")

</div>

Hello ,  
Sorry I forget to ask you why the variables in group tomcat1 apply to tomcat2 ? whereas I have two groups with different value of variables for tomcat’s path ?

---

<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:** [May 23, 2014, 12:18pm UTC](https://forum.ansible.com/t/inventory-file-different-groups-with-same-variables-but-not-the-same-value/15289/4 "2014-05-23T12:18:37Z")

</div>

“Sorry I forget to ask you why the variables in group tomcat1 apply to tomcat2 ?”

Because the host is in both groups.

Groups may cover things such as purpose, geographic location, hardware type, what team owns them, and more.

The “hosts:” directive merely selects what hosts are going to be targetted in the given play, and the variables come from all groups, with subgroups being able to override those of their parents (for instance, a north\_carolina server may specify [time.nc.example.com](http://time.nc.example.com) for NTP while a united\_states server might set a default of [time.ny.example.com](http://time.ny.example.com))

---

<div class="post-metadata">

**Author:** ![Sacha\_Vukovic](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@Sacha\_Vukovic](https://forum.ansible.com/u/Sacha_Vukovic)\
**Post date:** [May 26, 2014, 9:02am UTC](https://forum.ansible.com/t/inventory-file-different-groups-with-same-variables-but-not-the-same-value/15289/5 "2014-05-26T09:02:33Z")

</div>

Thank you for your answer ,

If I want to use tomcat’s role for two instances on same host is better to use group\_vars than the inventory file ?

---

<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:** [May 27, 2014, 8:11pm UTC](https://forum.ansible.com/t/inventory-file-different-groups-with-same-variables-but-not-the-same-value/15289/6 "2014-05-27T20:11:12Z")

</div>

"If I want to use tomcat’s role for two instances on same host is better to use group\_vars than the inventory file ? "

group\_vars and inventory variables are the same thing expressed two different ways.

If you want to use the role twice on the same box, this is a different pattern, which are parameterized roles:

Click [http://docs.ansible.com/playbooks\_roles.html#roles](http://docs.ansible.com/playbooks_roles.html#roles) and scroll down and it will talk about parameterized roles.

You can pass different parameters to the same role, applied twice, to the same server or set of servers.

---

<div class="post-metadata">

**Author:** ![Sacha\_Vukovic](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@Sacha\_Vukovic](https://forum.ansible.com/u/Sacha_Vukovic)\
**Post date:** [June 1, 2014, 2:13pm UTC](https://forum.ansible.com/t/inventory-file-different-groups-with-same-variables-but-not-the-same-value/15289/7 "2014-06-01T14:13:12Z")

</div>

Hello

Thank you for your answers, parameterized roles is a solution, but it’s actually not as simple as that when you have two environments . After reading your link I use vars\_files for the distinction made by environment

example site.yml:

> `
> 
> - hosts: tomcat1-hosts
> 
> user: root
> 
> vars\_files:
> 
> - [‘vars/tomcat1\_{{ env }}.yml’]
> 
> pre\_tasks:
> 
> - debug: msg={{product\_dir }}
> 
> roles:
> 
> - { role: tomcat, tags: tomcat }
> 
> - hosts: tomcat2-hosts
> 
> user: root
> 
> vars\_files:
> 
> - [‘vars/tomcat2\_{{ env }}.yml’]
> 
> pre\_tasks:
> 
> - debug: msg={{product\_dir }}
> 
> roles:
> 
> - { role: tomcat, tags: tomcat }  
> `
> 
> > >

My invetory file:  
`  
[tomcat1-hosts]  
tux1

[tomcat2-hosts]  
tux1

[all:children]  
tomcat1-hosts  
tomcat2-hosts

[all:vars]  
env=‘prod’

`

Playbook:

`

PLAY [tomcat1-hosts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK: [debug msg={{product\_dir}}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [tux1] =\> {  
“msg”: “/data/produits/tomcat1”  
}

PLAY [tomcat2-hosts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK: [debug msg={{product\_dir}}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [tux1] =\> {  
“msg”: “/data/produits/tomcat2”  
}

`

So I can deploy to different environments, the same application with different value for tomcat path, tomcat port. Thanks ansible for that 😉

---

<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:** [June 1, 2014, 7:07pm UTC](https://forum.ansible.com/t/inventory-file-different-groups-with-same-variables-but-not-the-same-value/15289/8 "2014-06-01T19:07:29Z")

</div>

If you’re doing vars\_files per environment, I’d highly recommend making the environment a group in inventory. This way it will automatically be loaded by group\_vars without the extra step of the parameterized vars\_file.
