# per-environment variables

**URL:** <https://forum.ansible.com/t/per-environment-variables/15128>\
**Category:** Ansible Project\
**Created:** [May 1, 2014, 9:40am UTC](https://forum.ansible.com/t/per-environment-variables/15128 "2014-05-01T09:40:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [May 1, 2014, 9:40am UTC](https://forum.ansible.com/t/per-environment-variables/15128/1 "2014-05-01T09:40:22Z")

</div>

I'm developing an ansible playbook in Vagrant and then  
applying it to a customer environment. Getting pretty far  
by using a single playbook (site.yml) and setting per-environment  
variables in different inventory files.

dev\_hosts = my dev. VMs  
live\_hosts = customer VM stack

then i can run

ansible-playbook -i (pick one) site.yml

and things by and large work great.

One problem I keep hitting is the 'live' environment needs everything  
to go via a proxy. live\_hosts has:

....  
[all:vars]  
http\_proxy=[http://custproxy:8080](http://custproxy:8080)  
....

and that's absent from dev\_hosts.

I use that to template /etc/profile.d/proxy.sh,  
so curl etc. are pre-set to use it. I'ts easy to do conditional  
logic in that template.

I'd like all the Ansible tasks that support an environment: field  
(git / get\_url / etc) to only have one set if that var is populated.

I can't just blindly set it or the tasks end up running with a proxy  
of nil/null/'' .

But I can't just add this to the inventory:

....  
[all:vars]  
http\_proxy=[http://custproxy:8080](http://custproxy:8080)  
proxy\_env:  
&nbsp;&nbsp;http\_proxy: {{http\_proxy}}  
....

because it just allows key=var style definitions.

Where's the earliest I can set this up?

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [May 1, 2014, 3:37pm UTC](https://forum.ansible.com/t/per-environment-variables/15128/2 "2014-05-01T15:37:37Z")

</div>

Dnia czw, 1 maj 2014, 11:40:22 Dick Davies pisze:

> I'm developing an ansible playbook in Vagrant and then  
> applying it to a customer environment. Getting pretty far  
> by using a single playbook (site.yml) and setting per-environment  
> variables in different inventory files.

Sounds like a bit of abuse of the inventory - I think it's only meant for listing hosts and connection details.

> dev\_hosts = my dev. VMs  
> live\_hosts = customer VM stack
> 
> then i can run
> 
> ansible-playbook -i (pick one) site.yml
> 
> and things by and large work great.
> 
> One problem I keep hitting is the 'live' environment needs everything  
> to go via a proxy. live\_hosts has:
> 
> ....  
> [all:vars]  
> http\_proxy=[http://custproxy:8080](http://custproxy:8080)  
> ....
> 
> and that's absent from dev\_hosts.
> 
> I use that to template /etc/profile.d/proxy.sh,  
> so curl etc. are pre-set to use it. I'ts easy to do conditional  
> logic in that template.
> 
> I'd like all the Ansible tasks that support an environment: field  
> (git / get\_url / etc) to only have one set if that var is populated.
> 
> I can't just blindly set it or the tasks end up running with a proxy  
> of nil/null/'' .
> 
> But I can't just add this to the inventory:
> 
> ....  
> [all:vars]  
> http\_proxy=[http://custproxy:8080](http://custproxy:8080)  
> proxy\_env:  
> &nbsp;&nbsp;&nbsp;http\_proxy: {{http\_proxy}}  
> ....
> 
> because it just allows key=var style definitions.
> 
> Where's the earliest I can set this up?

I'm not sure if that's the right way, but for environment-specific variables I'm using play's vars\_files.  
vagrant\_inventory:  
[all:vars]  
environment\_name=vagrant

site.yml:  
- hosts: some\_group  
&nbsp;&nbsp;&nbsp;vars\_files:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- vars/{{ environment\_name }}.yml

This way I have less clutter in inventory, and I can easily see the environment's global settings.  
It's also pretty handy for usage with Vault, by adding "- secrets/{{ environment\_name }}.yml" there (and as secrets for Vagrant aren't that secret, that gives you an index of required secrets too :-))

---

<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 1, 2014, 9:21pm UTC](https://forum.ansible.com/t/per-environment-variables/15128/3 "2014-05-01T21:21:09Z")

</div>

“Sounds like a bit of abuse of the inventory - I think it’s only meant for listing hosts and connection details.”

I strongly disagree.

Group based variables are quite useful and having a group\_vars/production and group\_vars/stage can be a great way to organize things.

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [May 3, 2014, 8:57am UTC](https://forum.ansible.com/t/per-environment-variables/15128/4 "2014-05-03T08:57:55Z")

</div>

In fairness, I probably am Doing It Wrong in some respects -  
I don't have prod/dev groups in this case, we're using a different  
inventory for prod and dev.

The alternative would be to have [prod\_mongos] , [prod\_mongors], [dev\_mongos]  
etc. etc. in the inventory which seems a bit cut n pasty to me.

With the 2 inventory approach, I can have the same groups across environments  
that differ only in FQDNs and number of members, and then an [all:vars] to hold  
environment-wide 'globals' (NTP settings, proxy, ansible\_ssh\* settings etc).

For the record :

I got this working cleanly (to my mind at least ) thanks to @drybjed  
on IRC pointing  
out set\_fact.

I have a roles/proxyenv/tasks/main.yml applied to all hosts  
that mpopulates a proxy\_env var using set\_fact + when  
and then all our yum/get\_url/gem etc. always have an environment:  
proxy\_env tacked onto them.

Environments that don't need to use a proxy simply don't set one in  
their inventory, and  
so that proxy\_env dict is empty for them.
