# Best way to import ENV variables with defaults

**URL:** <https://forum.ansible.com/t/best-way-to-import-env-variables-with-defaults/15703>\
**Category:** Ansible Project\
**Created:** [July 18, 2014, 1:02pm UTC](https://forum.ansible.com/t/best-way-to-import-env-variables-with-defaults/15703 "2014-07-18T13:02:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yasir\_Saleem](https://avatars.discourse-cdn.com/v4/letter/y/13edae/32.png) [@Yasir\_Saleem](https://forum.ansible.com/u/Yasir_Saleem)\
**Post date:** [July 18, 2014, 1:02pm UTC](https://forum.ansible.com/t/best-way-to-import-env-variables-with-defaults/15703/1 "2014-07-18T13:02:14Z")

</div>

Hi,  
I want to important env variables as fellow in a group\_vars file.:

project\_id: “{{ lookup(‘env’,‘PROJECT\_ID’) | default(‘FOO\_PROJECT’) }}”

I want it to pick default value if ENV variable is not defined. Default part is not working. Am I doing it right? Is there a better way to do it?

Another unrelated question:

What is difference between?

flag: ‘True’  
flag: True

Thanks,  
Yasir.

---

<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:** [July 18, 2014, 1:21pm UTC](https://forum.ansible.com/t/best-way-to-import-env-variables-with-defaults/15703/2 "2014-07-18T13:21:53Z")

</div>

(A)

The lookup plugin will return empty string, not None, if that env is not found.

However that is still False in Python. That’s curious and may be related to how “default” is implemented. Will have to try that.

(B)

In YAML, the first is a string, the second is a boolean.

As passed to arguments in ansible modules that take booleans, both will do the right thing.

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [July 18, 2014, 1:47pm UTC](https://forum.ansible.com/t/best-way-to-import-env-variables-with-defaults/15703/3 "2014-07-18T13:47:37Z")

</div>

Excerpts from Michael DeHaan's message of 2014-07-18 09:21:53 -0400:

> The lookup plugin will return empty string, not None, if that env is not  
> found.
> 
> However that is still False in Python. That's curious and may be related  
> to how "default" is implemented. Will have to try that.

Jinja's `default` filter will, by default, only use the value passed to  
to it if the variable in question is undefined, like, fo' real.

If you want to use the default value whenever the variable is False in  
a boolean context, then use:

&nbsp;&nbsp;&nbsp;&nbsp;my\_variable|default("my\_default", true)

Check it out: [http://jinja.pocoo.org/docs/templates/#builtin-filters](http://jinja.pocoo.org/docs/templates/#builtin-filters)

---

<div class="post-metadata">

**Author:** ![Yasir\_Saleem](https://avatars.discourse-cdn.com/v4/letter/y/13edae/32.png) [@Yasir\_Saleem](https://forum.ansible.com/u/Yasir_Saleem)\
**Post date:** [July 18, 2014, 2:03pm UTC](https://forum.ansible.com/t/best-way-to-import-env-variables-with-defaults/15703/4 "2014-07-18T14:03:02Z")

</div>

Morgan,

Thanks a lot, passing true to the ‘default’ did the trick. It works now!!

Working statement…

project\_id: “{{ lookup(‘env’,‘PROJECT\_ID’) | default(‘FOO\_PROJECT’,true) }}”

For Question B:

In when statement, which is preferred

when: flag is defined and flag == ‘False’  
Or  
when: flag is defined and flag == False

Are the equivalent in ansible?

Thanks.  
Yasir.

---

<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 18, 2014, 2:09pm UTC](https://forum.ansible.com/t/best-way-to-import-env-variables-with-defaults/15703/5 "2014-07-18T14:09:13Z")

</div>

Default is meant for a empty/undefined variable, lookup returns a value.

Brian Coca

---

<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:** [July 18, 2014, 6:11pm UTC](https://forum.ansible.com/t/best-way-to-import-env-variables-with-defaults/15703/6 "2014-07-18T18:11:35Z")

</div>

Nice find on the Jinja2 default behavior - thanks Morgan!
