# Different group vars based on the inventory, aka staging vs prod

**URL:** <https://forum.ansible.com/t/different-group-vars-based-on-the-inventory-aka-staging-vs-prod/14924>\
**Category:** Ansible Project\
**Created:** [April 2, 2014, 6:58pm UTC](https://forum.ansible.com/t/different-group-vars-based-on-the-inventory-aka-staging-vs-prod/14924 "2014-04-02T18:58:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kalman\_Speier](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@Kalman\_Speier](https://forum.ansible.com/u/Kalman_Speier)\
**Post date:** [April 2, 2014, 6:58pm UTC](https://forum.ansible.com/t/different-group-vars-based-on-the-inventory-aka-staging-vs-prod/14924/1 "2014-04-02T18:58:13Z")

</div>

Hi,

I have a repository with the same structure as the [best practices](http://docs.ansible.com/playbooks_best_practices.html#directory-layout) in the docs, with two inventory files (staging and production).  
My question is that how can I control the differences between those environments (stage vs prod) with group variables?

I know I can place vars into the inventory file, but I’m curious if there any nicer way to do this?

Also the [docs says](http://docs.ansible.com/intro_inventory.html#splitting-out-host-and-group-specific-data): “The preferred practice in Ansible is actually not to store variables in the inventory file.”

So I’m wondering is there any other solution, like having a “group\_vars/staging.yml” for example or anything else?

Many thanks,

Kalman Speier

---

<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:** [April 2, 2014, 8:32pm UTC](https://forum.ansible.com/t/different-group-vars-based-on-the-inventory-aka-staging-vs-prod/14924/2 "2014-04-02T20:32:14Z")

</div>

A good way to do this is to define two inventory files but put all production servers in a production group ALSO and all stage servers in a stage group.

This allows the following:

inventory/production  
inventory/stage  
inventory/group\_vars/all  
inventory/group\_vars/production  
inventory/group\_vars/stage

and just -i inventory/production

OR

-i inventory/stage

---

<div class="post-metadata">

**Author:** ![Kalman\_Speier](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@Kalman\_Speier](https://forum.ansible.com/u/Kalman_Speier)\
**Post date:** [April 3, 2014, 8:03am UTC](https://forum.ansible.com/t/different-group-vars-based-on-the-inventory-aka-staging-vs-prod/14924/3 "2014-04-03T08:03:20Z")

</div>

Hi Michael,

Cool, so my inventory files should look like this:

[webservers]

web01  
web02  
web03  
web04

[loadbalancers]  
lb01  
lb02

[staging:children]  
webservers  
loadbalancers

Is that correct?

Many thanks,  
Kalman Speier

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [April 3, 2014, 11:40am UTC](https://forum.ansible.com/t/different-group-vars-based-on-the-inventory-aka-staging-vs-prod/14924/4 "2014-04-03T11:40:07Z")

</div>

Hi Kalman,

I have to deal with similar challange as well.  
IMHO design is good however with complex configuraiton is starting quite challanging.  
We have much more environment to setup for the same product and is not easy to do that with inventories and groups.  
(for example we have Dev on local machines / Dev-Test on ESXi / Pre-production / Dedicated servers / RackSpace cloud.  
And each of this evnrionment have something different

- for example RackSpace cloud is behind NAT - so you can’t use IP from fact to update nagios rules on remote server, you must map this to exterrnal IP
- Dealing with apache vhosts configuration - different domains for internal testings and production environment.

Challnages which we have at this moment:

- Be sure which variable is used, Ansible documentation says that variable should be define only in one place, and with complex configuration this is not easy especially if environmentens are differnt.
- Detecting whether variable is defined or not before run plays (is quite hard to find out whehre I should put variable if nasible complains in the middle of runing - ‘variable’ not defined’

I am not saying that this is bad, etc. I am just saying that is challaning (at least for me) manage or variables in proper way (so far) to allow easy run playbook on all targets (producton / dev / test / etc).

So, have hope that we will find the right way.

PS.  
I am trying to find a way whether there is possible to force ansible to inform me about variable files which has been loaded.  
For example when you’re using ‘‘var\_files’’ with many files - search for first - ansible reports which file was readed: Loaded var file … CentOS.yml, would be very nice to have option to see this as well for default variables like group\_vars / hosts\_vars, etc.  
We will see what is possible to achieve.

Good luck.

Best regards,  
Marcin Praczko

---

<div class="post-metadata">

**Author:** ![Kalman\_Speier](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@Kalman\_Speier](https://forum.ansible.com/u/Kalman_Speier)\
**Post date:** [April 3, 2014, 12:04pm UTC](https://forum.ansible.com/t/different-group-vars-based-on-the-inventory-aka-staging-vs-prod/14924/5 "2014-04-03T12:04:01Z")

</div>

Hi,

I can imagine that, with more and more environments it can be tricky,  
however it seems we can easily manage our development, staging and production environemnts, with Michael’s latest advice.

Currently we have the following directory layout (roughly) and our inventory files looking like I wrote in my previous message:

.  
├── inventory

│ ├── group\_vars  
│ │ ├── all.yml  
│ │ ├── development.yml

│ │ ├── loadbalancers.yml

│ │ ├── production.yml

│ │ ├── staging.yml

│ │ └── webservers.yml  
│ ├── development  
│ ├── production  
│ └── staging  
├── roles  
│ ├── loadbalancer  
│ ├── webserver  
├── loadbalancers.yml

├── site.yml

└── webservers.yml

… and I can say that it’s working very well for us. 😉

According to your desire to find a way to get informed about loaded var files,  
maybe worth to take a look around Ansible callbacks, [here](http://www.ansible.com/blog/listen-to-your-servers-talk) is a nice post about it,  
not sure if is there a callback for this, it’s just an idea. 😉

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [April 3, 2014, 1:51pm UTC](https://forum.ansible.com/t/different-group-vars-based-on-the-inventory-aka-staging-vs-prod/14924/6 "2014-04-03T13:51:52Z")

</div>

Hi,

Only one questions.  
Is there any different for group\_vars whether are located in main root or in inventory?

/ Ansible main folder  
/ group\_vars  
/ inventory/group\_vars

Or this is the same behaviour and ansible is just searching both of them?

Thanks for sharing this example is really helpfull.

Best regards,  
Marcin Praczko

---

<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:** [April 3, 2014, 1:54pm UTC](https://forum.ansible.com/t/different-group-vars-based-on-the-inventory-aka-staging-vs-prod/14924/7 "2014-04-03T13:54:27Z")

</div>

​Yes.​
