Set a default value for a variable if ENV is empty

1,069 views
Skip to first unread message

Loren Gordon

unread,
Apr 19, 2016, 7:03:48 PM4/19/16
to Packer
Hello,
Is it possible to set a default value for a variable in a packer template, but override that default with an ENV? I'm thinking of something equivalent to the shell syntax, `var="${ENV:-default}"`. I've looked, but can't seem to find a way to do this...

Thanks,
-Loren

Brett Russ

unread,
Apr 19, 2016, 7:17:05 PM4/19/16
to Packer
On Tuesday, April 19, 2016 at 7:03:48 PM UTC-4, Loren Gordon wrote:
Hello,
Is it possible to set a default value for a variable in a packer template, but override that default with an ENV? I'm thinking of something equivalent to the shell syntax, `var="${ENV:-default}"`. I've looked, but can't seem to find a way to do this...

Could you move the entire problem to shell?
DFLT={{user `dflt`}}
VAR=${ENV:-DFLT}
then use the $VAR shell var? 

Loren Gordon

unread,
Apr 19, 2016, 7:21:37 PM4/19/16
to Packer
Interesting, thanks! I'll try it. It would be more elegant if it were natively supported in packer, but this might be good enough.

-Loren

Alvaro Miranda Aguilera

unread,
Apr 19, 2016, 9:34:09 PM4/19/16
to packe...@googlegroups.com
Hello,

If you define

key=value in the variables block, thats the default

then can be overrided with

packer build -var "key=newvalue" template.json

or

packer build -var "key=${var}" template.json

is this what you are after?

if not, can you explain more?

alvaro.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/5b4f8a84-f18b-4e7d-b02e-248060a27e2e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Loren Gordon

unread,
Apr 19, 2016, 9:48:14 PM4/19/16
to Packer
No, not quite. What I'm looking to do is define a default value, which may be overridden by an ENV, which may be overridden by a command line variable.

For example, I can assign a default value to a packer variable, "myvar", like so:

"variables": {
        "myvar": "default_value"
}

Or, I can tell packer to read an env named MYVAR and assign it to the variable "myvar", like so:

"variables": {
        "myvar": "{{ env `MYVAR` }}"
}

However, with this method there is no default value.

For either method, I can also use "-var" on the command line to assign a value to the variable. 

What I'm looking to do is use the {{ env }} function, but _also_ assign a default value if the ENV is empty. I cannot find a way to do that within packer itself.

-Loren

Alvaro Miranda Aguilera

unread,
Apr 19, 2016, 11:19:37 PM4/19/16
to packe...@googlegroups.com
Hello,

Not possible as today.

2 Ways of doing it.

You could have a json file with vars.

myenv.json or mykey.json and use it at run time.

other way would be

in a variable block:

"myvar": "mydefault",
"myvarenv": "ENV[myvar]"

you pass both to the shell script and inside the shell you may check.

if [ "${myvarenv}" ]; then
  localvar="${myvarenv}"
elif [ "${myvar}" ]; then
  localvar="${myvar}"
fi

Unfortunately, i don't see other way as today.

Alvaro.


Loren Gordon

unread,
Apr 20, 2016, 8:15:16 AM4/20/16
to Packer
Ok, thanks, I filed a feature request.


-Loren
Reply all
Reply to author
Forward
0 new messages