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.
(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.
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,
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.
Default is meant for a empty/undefined variable, lookup returns a value.
Brian Coca
Nice find on the Jinja2 default behavior - thanks Morgan!