hashicorp/packer · error

Error interpolating Inline: %s

Error message

Error interpolating Inline: %s

What it means

This error means Packer could not render one of the shell provisioner's `inline` command strings through the HCL2/template interpolation engine. interpolate.Render evaluates variables, functions, and user variables in the command text; a failure here (bad function call, unknown variable, wrong argument types) aborts the whole Provision run before any script file is written. The original interpolation error is appended to the message.

Source

Thrown at provisioner/shell/provisioner.go:217

	// If we have an inline script, then turn that into a temporary
	// shell script and use that.
	if p.config.Inline != nil {
		tf, err := tmp.File("packer-shell")
		if err != nil {
			return fmt.Errorf("Error preparing shell script: %s", err)
		}
		defer os.Remove(tf.Name())

		// Set the path to the temporary file
		scripts = append(scripts, tf.Name())

		// Write all inline commands to this buffer
		commandBuffer := strings.Builder{}
		for _, command := range p.config.Inline {
			p.config.ctx.Data = generatedData
			command, err := interpolate.Render(command, &p.config.ctx)
			if err != nil {
				return fmt.Errorf("Error interpolating Inline: %s", err)
			}
			if _, err := fmt.Fprintf(&commandBuffer, "%s\n", command); err != nil {
				return fmt.Errorf("Error preparing shell script: %s", err)
			}
		}

		// If the user has defined an inline shebang, use that.
		// Or If command does not start with a shebang, use the default shebang.
		// else command already has a shebang, so do not write it.
		if p.config.inlineShebangDefined || !strings.HasPrefix(commandBuffer.String(), "#!") {
			if _, err := fmt.Fprintf(tf, "#!%s\n", p.config.InlineShebang); err != nil {
				return fmt.Errorf("Error preparing shell script: %s", err)
			}
		}

		// Write the collected commands to the file
		if _, err := tf.WriteString(commandBuffer.String()); err != nil {
			return fmt.Errorf("Error preparing shell script: %s", err)

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Fix the template syntax reported by the wrapped error: quote it as a literal by escaping `$` as `$${...}` when you want shell expansion, not Packer interpolation
  2. Define or correct the referenced variable/local so `var.x`, `local.y`, or `build.z` exists at provision time
  3. Validate the template with `packer init` + `packer validate` before running the build
  4. If the value comes from generatedData (e.g. build), confirm the key is produced by an earlier builder/data source in the same build

Example fix

// before
inline = ["echo ${undefined_var}"]
// after
inline = ["echo ${var.my_var}"]  // or escape shell syntax: "echo $${SOME_SHELL_VAR}"
Defensive patterns

Strategy: validation

Validate before calling

# before running the build
packer validate template.pkr.hcl
# also escape shell expansions: $${VAR} instead of ${VAR} inside inline

Prevention

When it happens

Trigger: Calling the shell provisioner's Provision with an `inline` entry containing invalid template syntax: an undefined `var.foo`, a misused template function like `${lower(...)}` instead of `lower(...)`, unbalanced `${`, or a function returning an error for the given generatedData (e.g. referencing a build-time key that does not exist).

Common situations: Typing `${build.ID}` when the data source key is actually `build.Name`; forgetting to define a variable in `variables`/`locals`; copy-pasting shell `${VAR}` expansion into HCL where Packer tries to interpret it; upgrading Packer and a deprecated template function disappearing.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/315409cf0eafc4d6. Report an issue: GitHub.