hashicorp/packer · error
Environment variable not in format 'key=value': %s
Error message
Environment variable not in format 'key=value': %s
What it means
The shell provisioner validates that every entry in the vars configuration is in 'key=value' form, since each is later exported as an environment variable on the guest. An entry with no '=' or an empty key fails validation and is collected into Prepare's error list.
Source
Thrown at provisioner/shell/provisioner.go:179
errors.New("Either a script file or inline script must be specified."))
} else if len(p.config.Scripts) > 0 && p.config.Inline != nil {
errs = packersdk.MultiErrorAppend(errs,
errors.New("Only a script file or an inline script can be specified, not both."))
}
for _, path := range p.config.Scripts {
if _, err := os.Stat(path); err != nil {
errs = packersdk.MultiErrorAppend(errs,
fmt.Errorf("Bad script '%s': %s", path, err))
}
}
// Do a check for bad environment variables, such as '=foo', 'foobar'
for _, kv := range p.config.Vars {
vs := strings.SplitN(kv, "=", 2)
if len(vs) != 2 || vs[0] == "" {
errs = packersdk.MultiErrorAppend(errs,
fmt.Errorf("Environment variable not in format 'key=value': %s", kv))
}
}
if errs != nil && len(errs.Errors) > 0 {
return errs
}
return nil
}
func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, generatedData map[string]interface{}) error {
if generatedData == nil {
generatedData = make(map[string]interface{})
}
p.generatedData = generatedData
scripts := make([]string, len(p.config.Scripts))
copy(scripts, p.config.Scripts)View on GitHub (pinned to eb36e3c3e4)
Solutions
- Rewrite each vars entry as KEY=value with a non-empty key.
- Values may contain '='; only the first '=' separates key and value — ensure the key part before it is valid.
- Remove empty or placeholder strings from the vars array.
- If you meant host environment variables, interpolate them in the template (e.g. HCL2 ${env.VAR}) instead of putting bare names in vars.
Example fix
// before "vars": ["MY_VAR", "=oops"] // after "vars": ["MY_VAR=value"]
Defensive patterns
Strategy: validation
Validate before calling
# shell: every vars entry must match key=value with a non-empty key for kv in $VARS; do echo "$kv" | grep -qE '^[^=]+=.*' || echo "bad var: $kv"; done
Prevention
- Always write vars entries as KEY=value.
- Remove empty strings from programmatic vars arrays.
- Interpolate host env vars in the template instead of bare names in vars.
- Remember values may contain '='; only the first '=' splits.
When it happens
Trigger: vars contains entries like 'foo' (no =), '=value' (empty key), or whitespace/empty strings in the Vars list.
Common situations: Passing env var names only (assuming Packer reads them from the host); quoting mistakes in JSON/HCL that drop the '='; building vars arrays programmatically and including empty elements; values containing '=' are fine (SplitN with limit 2), but keys with spaces or leading '=' are not.
Related errors
- Bad script '%s': %s
- ErrInvalidCompressionLevel
- Error parsing target template: %s
- error parsing output_dir template: %w
- signer and key must match when both are set
AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05).
Data as JSON: /api/errors/f02fde27cdfcebd9.
Report an issue: GitHub.