# Defaulting variables in Packer Template?

**URL:** <https://discuss.hashicorp.com/t/defaulting-variables-in-packer-template/7525>\
**Category:** Packer\
**Created:** [April 9, 2020, 3:18am UTC](https://discuss.hashicorp.com/t/defaulting-variables-in-packer-template/7525 "2020-04-09T03:18:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![carsoni](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/carsoni/32/5900_2.png) [@carsoni](https://discuss.hashicorp.com/u/carsoni)\
**Post date:** [April 9, 2020, 3:18am UTC](https://discuss.hashicorp.com/t/defaulting-variables-in-packer-template/7525/1 "2020-04-09T03:18:52Z")

</div>

Hi Guys,

I’m running a packer build through a Gitlab CICD pipline.

Gitlab provides the environment variables during the run so no problems there.

However during the template coding phase I want to be able to run a local task (VSCode task) to validate the json I am writing without having to commit. Unfortunately all the variables which would normally be populated by Gitlab are coming up blank.

Is there a syntax to allow a default value to be substituted if needed like is possible in bash?

Something like…

```
"checksum_type":"${CHECKSUM_TYPE:-sha256}"

```

Thanks in advance  
Regards  
Ian Carson

---

<div class="post-metadata">

**Author:** ![blake](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/blake/32/27620_2.png) [@blake](https://discuss.hashicorp.com/u/blake)\
**Post date:** [April 9, 2020, 3:57am UTC](https://discuss.hashicorp.com/t/defaulting-variables-in-packer-template/7525/2 "2020-04-09T03:57:42Z")

</div>

Hi @carsoni,

Welcome to our community! 👋

Packer supports setting default values for [User Variables](https://packer.io/docs/templates/user-variables.html). Those values can be overridden when building an image using either the `-var` or `-var-file` options.

Packer does support using [Environment Variables](https://packer.io/docs/templates/user-variables.html#environment-variables) in your User Variables. However, the value of a user variables will default to a blank string if the referenced environment variable is not set. As a workaround, you could first generate a JSON `-var-file` using values from your environment, and then provide that file to Packer during the build process.

---

<div class="post-metadata">

**Author:** ![carsoni](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/carsoni/32/5900_2.png) [@carsoni](https://discuss.hashicorp.com/u/carsoni)\
**Post date:** [April 9, 2020, 5:24am UTC](https://discuss.hashicorp.com/t/defaulting-variables-in-packer-template/7525/3 "2020-04-09T05:24:39Z")

</div>

Thanks for that @blake😀  
Looks like

```
{
    "checksum_type": "sha256"
}

```

as a var-file passed to my validate task has done the trick. It overwrites the variable in the packer template while leaving all the others to be used.

Nice one!

Regards  
Ian
