Best way to import ENV variables with defaults

3,056 views
Skip to first unread message

Yasir Saleem

unread,
Jul 18, 2014, 9:02:14 AM7/18/14
to ansible...@googlegroups.com
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.

Michael DeHaan

unread,
Jul 18, 2014, 9:21:57 AM7/18/14
to ansible...@googlegroups.com
(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.



 



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b238a149-c601-4329-be35-199296d23729%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

C. Morgan Hamill

unread,
Jul 18, 2014, 9:47:42 AM7/18/14
to ansible-project
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:

my_variable|default("my_default", true)

Check it out: http://jinja.pocoo.org/docs/templates/#builtin-filters
--
Morgan Hamill

Yasir Saleem

unread,
Jul 18, 2014, 10:03:15 AM7/18/14
to ansible...@googlegroups.com
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.

Brian Coca

unread,
Jul 18, 2014, 10:09:17 AM7/18/14
to ansible...@googlegroups.com

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

Brian Coca

Michael DeHaan

unread,
Jul 18, 2014, 2:11:38 PM7/18/14
to ansible...@googlegroups.com
Nice find on the Jinja2 default behavior - thanks Morgan!


On Fri, Jul 18, 2014 at 10:09 AM, Brian Coca <bria...@gmail.com> wrote:

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

Brian Coca

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages