hashicorp/packer · error

Error preparing shell script: %s

Error message

Error preparing shell script: %s

What it means

When the shell provisioner is configured with inline scripts, Provision creates a temporary file (tmp.File("packer-shell")) to hold the joined inline commands. If creating that temp file fails, the error wraps the OS-level cause and provisioning aborts before any script runs.

Source

Thrown at provisioner/shell/provisioner.go:204

	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)

	// 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)
			}
		}

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Check the wrapped cause and verify the host temp directory exists and is writable (echo $TMPDIR; touch $TMPDIR/test).
  2. Free disk space / inodes on the partition holding the temp directory.
  3. Set TMPDIR (or TMP/TEMP on Windows) to a writable location when the default is restricted.
  4. Alternatively use a scripts file instead of inline to avoid temp-file creation.

Example fix

// before (shell)
TMPDIR=/nonexistent packer build template.pkr.hcl
// after
TMPDIR=/var/tmp packer build template.pkr.hcl
Defensive patterns

Strategy: validation

Validate before calling

# shell: confirm temp dir is writable before building
test -d "$TMPDIR" && test -w "$TMPDIR" && touch "$TMPDIR/.writecheck" && rm "$TMPDIR/.writecheck" || echo "TMPDIR not writable"

Prevention

When it happens

Trigger: tmp.File fails because the host temp directory (TMPDIR/TMP/TEMP or /tmp) is missing, full, has wrong permissions, or the process lacks write access; also possible under restrictive sandboxing or noexec/filled inodes.

Common situations: CI containers with read-only /tmp or tiny tmpfs; Packer run as a restricted user; TMPDIR pointing to a nonexistent directory; disk quota exhausted on the host.

Related errors


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