Post

Packer - whitespace in env vars

When setting environment variables Packer, make sure there’s no space in between. If there’s a whitespace, Packer passes on that whitespace within the value of the env var.

Using # as a marker, we can see what value our env var will look like:

1
2
3
4
5
6
7
8
9
10
11
  provisioner "powershell" {
    environment_vars = [
      "ENV_VAR_WITHOUT_SPACE=${var.tmp_folder}",
      "ENV_VAR_WITH_SPACE = ${var.tmp_folder}"
    ]
    inline = [
      "$ErrorActionPreference='Stop'",
      "Write-Host \"EnvVarWithoutSpace:#$Env:ENV_VAR_WITHOUT_SPACE\"",
      "Write-Host \"EnvVarWithSpace:#$Env:ENV_VAR_WITH_SPACE\""
    ]
  }

output is

1
2
3
. . .
EnvVarWithoutSpace:#C:\tmp\packer-env-var
EnvVarWithSpace:# C:\tmp\packer-env-var

Another way to deal with this is to call the Trim method before processing those env vars:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# check if they exist
$expectedEnvVars = @(
    $env:ENV_VAR_WITHOUT_SPACE, 
    $env:ENV_VAR_WITH_SPACE
)
foreach ($envVar in $expectedEnvVars) {
    if ([string]::IsNullOrWhiteSpace($envVar)) {
        throw "$envVar is null or is empty" 
    }
}
$MyVar1 = ($env:ENV_VAR_WITHOUT_SPACE).Trim()
$MyVar2 = ($env:ENV_VAR_WITH_SPACE).Trim()

# do something with $MyVar1 and $MyVar2
This post is licensed under CC BY 4.0 by the author.