{"record":{"id":"c8a433a8d3947075","repo":"kubernetes/kops","slug":"unable-to-marhshal-formatted-content-to-yaml-s","errorCode":null,"errorMessage":"unable to marhshal formatted content to yaml: %s","messagePattern":"unable to marhshal formatted content to yaml: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/toolbox_template.go","lineNumber":206,"sourceCode":"\t\t\tcontinue\n\t\t}\n\n\t\tif !options.formatYAML {\n\t\t\tdocuments = append(documents, strings.Split(rendered, \"---\\n\")...)\n\t\t\tcontinue\n\t\t}\n\n\t\tfor _, x := range strings.Split(rendered, \"---\\n\") {\n\t\t\tvar data map[string]interface{}\n\t\t\tif err := yaml.Unmarshal([]byte(x), &data); err != nil {\n\t\t\t\treturn fmt.Errorf(\"unable to unmarshall content from template: %s, error: %s\", x, err)\n\t\t\t}\n\t\t\tif len(data) <= 0 {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tformatted, err := yaml.Marshal(&data)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"unable to marhshal formatted content to yaml: %s\", err)\n\t\t\t}\n\t\t\tdocuments = append(documents, string(formatted))\n\t\t}\n\t}\n\t// join in harmony all the YAML documents back together\n\tcontent := strings.Join(documents, \"---\\n\")\n\n\tiowriter := out\n\t// @check if we are writing to a file rather than stdout\n\tif options.outputPath != \"\" {\n\t\tw, err := os.OpenFile(utils.ExpandPath(options.outputPath), os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0o660)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to open file: %s, error: %v\", options.outputPath, err)\n\t\t}\n\t\tdefer try.CloseFile(w)\n\t\tiowriter = w\n\t}\n","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/toolbox_template.go#L188-L224","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the wrapped error to identify the unmarshalable value and fix the offending value/template.","Simplify or correct --set / --values inputs so rendered output is plain YAML-compatible data.","Update kops to the latest patch release in case the yaml library behavior changed."],"exampleFix":"// before\nvalues.yaml contains a key with a value that round-trips into an invalid type\n// after\nfix values.yaml so all values are scalars, lists or maps:\nreplicas: 3\nname: my-app","handlingStrategy":"try-catch","validationCode":"if _, err := yaml.Marshal(&data); err != nil { /* fix values before running toolbox template */ }","typeGuard":null,"tryCatchPattern":"out, err := exec.Command(\"kops\", \"toolbox\", \"template\", ...).CombinedOutput()\nif strings.Contains(string(out), \"unable to marhshal formatted content to yaml\") { inspect --set/--values inputs }","preventionTips":["Keep --values files to plain scalars, lists and maps","Validate rendered templates locally before scripting around them","Keep kops updated for yaml library fixes"],"tags":["cli","yaml","template"],"backgroundTag":"yaml-marshal-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}