kubernetes/kops · error

unable to marhshal formatted content to yaml: %s

Error message

unable to marhshal formatted content to yaml: %s

What it means

RunToolBoxTemplate renders a template with the template context, splitting documents and re-marshaling each with sigs.k8s.io/yaml. If yaml.Marshal fails on the rendered data, the command aborts with this message wrapping the underlying error. It indicates the data (after template rendering) cannot be represented as YAML.

Source

Thrown at cmd/kops/toolbox_template.go:206

			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)
		if err != nil {
			return fmt.Errorf("unable to open file: %s, error: %v", options.outputPath, err)
		}
		defer try.CloseFile(w)
		iowriter = w
	}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Read the wrapped error to identify the unmarshalable value and fix the offending value/template.
  2. Simplify or correct --set / --values inputs so rendered output is plain YAML-compatible data.
  3. Update kops to the latest patch release in case the yaml library behavior changed.

Example fix

// before
values.yaml contains a key with a value that round-trips into an invalid type
// after
fix values.yaml so all values are scalars, lists or maps:
replicas: 3
name: my-app
Defensive patterns

Strategy: try-catch

Validate before calling

if _, err := yaml.Marshal(&data); err != nil { /* fix values before running toolbox template */ }

Try / catch

out, err := exec.Command("kops", "toolbox", "template", ...).CombinedOutput()
if strings.Contains(string(out), "unable to marhshal formatted content to yaml") { inspect --set/--values inputs }

Prevention

When it happens

Trigger: Running `kops toolbox template ...` where a rendered template document produces data that yaml.Marshal rejects, e.g. data containing values of an unmarshalable Go type or a marshal failure from the yaml library.

Common situations: Custom values (--set/--values) producing exotic types that the template engine emits in a form yaml.Marshal cannot handle; corrupted values files; unusual nested structures.

Related errors


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