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
- Check the wrapped cause and verify the host temp directory exists and is writable (echo $TMPDIR; touch $TMPDIR/test).
- Free disk space / inodes on the partition holding the temp directory.
- Set TMPDIR (or TMP/TEMP on Windows) to a writable location when the default is restricted.
- 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
- Verify TMPDIR/TEMP points to an existing writable directory in CI images.
- Watch disk space and inode usage on the temp partition.
- Prefer scripts files over inline in sandboxes with restricted /tmp.
- Run builds as a user with temp-directory write access.
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
- failed to create internal temporary file for Packer SBOM: %s
- failed to close temporary file for Packer SBOM %s: %s
- failed to open Packer SBOM file %q: %s
- failed to create temp file: %w
- failed to close temp file: %w
AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05).
Data as JSON: /api/errors/ae2880d761f9fd32.
Report an issue: GitHub.