# Using environment vars for variables

**URL:** <https://forum.ansible.com/t/using-environment-vars-for-variables/15725>\
**Category:** Ansible Project\
**Created:** [July 21, 2014, 5:59pm UTC](https://forum.ansible.com/t/using-environment-vars-for-variables/15725 "2014-07-21T17:59:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Hall](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@Tom\_Hall](https://forum.ansible.com/u/Tom_Hall)\
**Post date:** [July 21, 2014, 5:59pm UTC](https://forum.ansible.com/t/using-environment-vars-for-variables/15725/1 "2014-07-21T17:59:15Z")

</div>

I thought

- name: Clone repo  
git: repo=“{{ git\_repo }}”  
dest=“{{ git\_dest }}”  
version=“{{ git\_version | lookup(‘env’,‘git\_version’) | default(‘master’) }}”  
notify: reload apache

would work to use the git\_version group\_var, ENV if it was set and master otherwise.

I think I was thinking of | as or rather than a filter when I first wrote it but still can’t figure out how to do what I want, weirdly the error is

stderr: fatal: ‘{{git\_repo}}’ does not appear to be a git repository

so I think the whole Jinja templating fails.

If I do  
lookup(‘env’,‘git\_version’) | default(‘master’)

It gets ‘’ if the env var is not set, rather then taking the default as I hoped

Thanks,  
Tom

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 21, 2014, 6:18pm UTC](https://forum.ansible.com/t/using-environment-vars-for-variables/15725/2 "2014-07-21T18:18:55Z")

</div>

this is not a valid construct, lookup is not a filter.

---

<div class="post-metadata">

**Author:** ![Tom\_Hall](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@Tom\_Hall](https://forum.ansible.com/u/Tom_Hall)\
**Post date:** [July 21, 2014, 6:24pm UTC](https://forum.ansible.com/t/using-environment-vars-for-variables/15725/3 "2014-07-21T18:24:27Z")

</div>

Thanks. Feels like what I want to achieve should be possible: fetch from group\_vars, override with ENV (for rollback perhaps), default to a branch. I could do it easily in python but we have jinja and yaml

Tom

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 21, 2014, 6:36pm UTC](https://forum.ansible.com/t/using-environment-vars-for-variables/15725/4 "2014-07-21T18:36:56Z")

</div>

version=“{{ ( lookup(‘env’,‘git\_version’)|default(git\_version)) | default(‘master’) }}”  
​

---

<div class="post-metadata">

**Author:** ![Tom\_Hall](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@Tom\_Hall](https://forum.ansible.com/u/Tom_Hall)\
**Post date:** [July 21, 2014, 7:39pm UTC](https://forum.ansible.com/t/using-environment-vars-for-variables/15725/5 "2014-07-21T19:39:58Z")

</div>

That does not work,  
msg: Could not determine remote revision for  
(presumably the lookup returns the empty string?)

I dont have git\_version set in my group\_var's

Tom

---

<div class="post-metadata">

**Author:** ![Tom\_Hall](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@Tom\_Hall](https://forum.ansible.com/u/Tom_Hall)\
**Post date:** [July 21, 2014, 7:41pm UTC](https://forum.ansible.com/t/using-environment-vars-for-variables/15725/6 "2014-07-21T19:41:04Z")

</div>

Think that's the case as  
lookup('env','git\_version') | default('master')  
has the same error.

---

<div class="post-metadata">

**Author:** ![Tom\_Hall](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@Tom\_Hall](https://forum.ansible.com/u/Tom_Hall)\
**Post date:** [July 21, 2014, 8:28pm UTC](https://forum.ansible.com/t/using-environment-vars-for-variables/15725/7 "2014-07-21T20:28:30Z")

</div>

I have a workaround, I can do  
--extra-vars "git\_version=HEAD~1"  
to rollback

Tom

---

<div class="post-metadata">

**Author:** ![Brian\_Spacecraft](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@Brian\_Spacecraft](https://forum.ansible.com/u/Brian_Spacecraft)\
**Post date:** [May 21, 2015, 3:22pm UTC](https://forum.ansible.com/t/using-environment-vars-for-variables/15725/8 "2015-05-21T15:22:43Z")

</div>

I just wanted to add a note that

lookup(‘env’, ‘ENV\_VAR’) | default(‘foo’)

returns an empty string if ENV\_VAR is not defined in the environment, but if you use

lookup(‘env’, ‘ENV\_VAR’) | default(‘foo’, True)

then if ENV\_VAR is not defined (in which case the env lookup plugin returns an empty string), the second parameter to the default filter tells it to treat any falsy value as undefined.
