hashicorp/nomad · error

A template must be supplied using '-template' when using go-

Error message

A template must be supplied using '-template' when using go-template formatting

What it means

The Nomad CLI 'var put' (and similar) command validates the -out format flag via validateOutputFlag. When 'go-template' is chosen as the output format, a corresponding -template string must also be supplied; otherwise the command aborts with this error. It exists to prevent rendering with an empty template, which would produce useless empty output.

Source

Thrown at command/var_put.go:608

func (c *VarPutCommand) validateInputFlag() error {
	switch c.inFmt {
	case "hcl", "json":
		return nil
	default:
		return errors.New(errInvalidInFormat)
	}
}

func (c *VarPutCommand) validateOutputFlag() error {
	if c.outFmt != "go-template" && c.tmpl != "" {
		return errors.New(errUnexpectedTemplate)
	}
	switch c.outFmt {
	case "none", "json", "hcl", "table":
		return nil
	case "go-template":
		if c.tmpl == "" {
			return errors.New(errMissingTemplate)
		}
		return nil
	default:
		return errors.New(errInvalidOutFormat)
	}
}

func warnInvalidIdentifier(in string) error {
	invalid := invalidIdentifier.FindAllString(in, -1)
	if len(invalid) == 0 {
		return nil
	}

	// Use %s instead of %q to avoid escaping characters.
	return fmt.Errorf(
		`"%s" contains characters %s that require the 'index' function for direct access in templates`,
		in,
		formatInvalidVarKeyChars(invalid),

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Add a non-empty -template flag, e.g. `-out go-template -template '{{.Key}}'`
  2. If you don't need templating, use one of the built-in formats: -out table, json, hcl, or none
  3. Fix the calling script to pass -template whenever -out is go-template

Example fix

// before
nomad var put -out go-template app/secret key=value
// after
nomad var put -out go-template -template '{{ range .Items }}{{ .Key }}={{ .Value }}{{ end }}' app/secret key=value
Defensive patterns

Strategy: validation

Validate before calling

out, tmpl := flags.GetString("out"), flags.GetString("template")
if out == "go-template" && strings.TrimSpace(tmpl) == "" {
    return fmt.Errorf("-template is required when -out=go-template")
}

Prevention

When it happens

Trigger: Running a command with `-out=go-template` (or `outFmt="go-template"`) while `c.tmpl` is empty, i.e. the `-template` flag was not provided or was set to an empty string.

Common situations: Users switching output format to go-template and forgetting the -template flag; scripts setting -out conditionally but always omitting -template; copy-pasted commands where -template was accidentally dropped.

Understand the failure class

Background: "--flag is required" and "must specify" CLI errors: how missing-required-flag validation works and how to fix it — this error's family across 20 libraries.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/dff91835c71853ff. Report an issue: GitHub.