Using environment vars for variables

103 views
Skip to first unread message

Tom Hall

unread,
Jul 21, 2014, 1:59:15 PM7/21/14
to ansible...@googlegroups.com
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

Brian Coca

unread,
Jul 21, 2014, 2:19:01 PM7/21/14
to ansible...@googlegroups.com

On Mon, Jul 21, 2014 at 1:59 PM, Tom Hall <thatto...@gmail.com> wrote:
{{ git_version | lookup('env','git_version') | default('master') }}"

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


--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

Tom Hall

unread,
Jul 21, 2014, 2:24:31 PM7/21/14
to ansible...@googlegroups.com

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

--
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/CADn%2BHszqaBdjXnjTs_V63ufFCJDv65kXc4DL%2B3wW0NOen50qOw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Jul 21, 2014, 2:37:00 PM7/21/14
to ansible...@googlegroups.com
 version="{{ ( lookup('env','git_version')|default(git_version)) | default('master') }}"

Tom Hall

unread,
Jul 21, 2014, 3:40:25 PM7/21/14
to ansible...@googlegroups.com
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

On 21 July 2014 19:36, Brian Coca <bria...@gmail.com> wrote:
> version="{{ ( lookup('env','git_version')|default(git_version)) |
> default('master') }}"
>
> --
> 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/CADn%2BHsy2kcdSkDN4UUOkc%2B-Hc1b9V-rL3o_fDOkf_Ph7r_%3DH3w%40mail.gmail.com.

Tom Hall

unread,
Jul 21, 2014, 3:41:27 PM7/21/14
to ansible...@googlegroups.com
Think that's the case as
lookup('env','git_version') | default('master')
has the same error.

Tom Hall

unread,
Jul 21, 2014, 4:28:55 PM7/21/14
to ansible...@googlegroups.com
I have a workaround, I can do
--extra-vars "git_version=HEAD~1"
to rollback

Tom

Brian Spacecraft

unread,
May 21, 2015, 1:46:12 PM5/21/15
to ansible...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages