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

Hi,

I have a repository with the same structure as the best practices 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: “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

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

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

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

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. :wink:

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 is a nice post about it,
not sure if is there a callback for this, it’s just an idea. :wink:

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

​Yes.​