kubernetes/kops · error

unable to unmarshall content from template

Error message

unable to unmarshall content from template: %s, error: %s

What it means

Returned by RunToolBoxTemplate when a rendered template document fails yaml.Unmarshal into map[string]interface{} during --format-yAML processing. After rendering, the output is split on "---\n" and each chunk must be a YAML mapping; invalid YAML syntax, non-mapping documents (scalars/lists), or template artifacts that render to garbage trigger this error. The failing chunk is embedded verbatim in the message.

Solutions

  1. Read the offending chunk in the error message and fix the YAML syntax at that point (indentation with spaces, quote strings containing : or #)
  2. Ensure each document between --- separators is a YAML mapping (key: value), not a scalar or list
  3. Quote values substituted into the template if they may contain YAML special characters
  4. Temporarily drop --format-yAML to see the raw rendered output and diagnose what the substitution produced
  5. Validate the rendered output with a linter such as yamllint

Example fix

// before (rendered doc)
metadata:
	name: my-cluster

// after
metadata:
  name: my-cluster
Defensive patterns

Strategy: validation

Validate before calling

for _, doc := range strings.Split(rendered, "---\n") {
    var m map[string]interface{}
    if err := yaml.Unmarshal([]byte(doc), &m); err != nil {
        return fmt.Errorf("rendered chunk is not a YAML mapping: %v", err)
    }
}

Type guard

func isYAMLMapping(doc string) bool {
    var m map[string]interface{}
    return yaml.Unmarshal([]byte(doc), &m) == nil
}

Try / catch

if err := yaml.Unmarshal([]byte(doc), &data); err != nil {
    return fmt.Errorf("document %q is not a YAML mapping (check tabs, unquoted special chars): %w", doc, err)
}

Prevention

When it happens

Trigger: --format-yAML is used and a rendered document chunk contains invalid YAML (tabs for indentation, duplicate keys, bad escapes) or is a non-mapping (a bare scalar, a list at top level), including chunks where the template rendered unexpected content between --- separators.

Common situations: Rendered values injecting special characters (colons in unquoted strings, "#" starting comments) that break YAML; tab indentation introduced by values substitution; templates whose sections between --- render to empty-but-nonempty whitespace or scalar output; assuming formatYAML can reformat non-map documents.

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/bf2463725568391e. Report an issue: GitHub.

Appendix: source

Thrown at cmd/kops/toolbox_template.go:199

		rendered, err := r.Render(string(content), context, snippets, options.failOnMissing)
		if err != nil {
			return fmt.Errorf("unable to render template: %s, error: %s", x, err)
		}
		// @check if the content is zero ignore it
		if len(rendered) <= 0 {
			continue
		}

		if !options.formatYAML {
			documents = append(documents, strings.Split(rendered, "---\n")...)
			continue
		}

		for _, x := range strings.Split(rendered, "---\n") {
			var data map[string]interface{}
			if err := yaml.Unmarshal([]byte(x), &data); err != nil {
				return fmt.Errorf("unable to unmarshall content from template: %s, error: %s", x, err)
			}
			if len(data) <= 0 {
				continue
			}
			formatted, err := yaml.Marshal(&data)
			if err != nil {
				return fmt.Errorf("unable to marhshal formatted content to yaml: %s", err)
			}
			documents = append(documents, string(formatted))
		}
	}
	// join in harmony all the YAML documents back together
	content := strings.Join(documents, "---\n")

	iowriter := out
	// @check if we are writing to a file rather than stdout
	if options.outputPath != "" {
		w, err := os.OpenFile(utils.ExpandPath(options.outputPath), os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0o660)

View on GitHub (pinned to 4c8573c808)