{"record":{"id":"bf2463725568391e","repo":"kubernetes/kops","slug":"unable-to-unmarshall-content-from-template-s-er","errorCode":null,"errorMessage":"unable to unmarshall content from template: %s, error: %s","messagePattern":"unable to unmarshall content from template: (.+?), error: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/toolbox_template.go","lineNumber":199,"sourceCode":"\n\t\trendered, err := r.Render(string(content), context, snippets, options.failOnMissing)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to render template: %s, error: %s\", x, err)\n\t\t}\n\t\t// @check if the content is zero ignore it\n\t\tif len(rendered) <= 0 {\n\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)","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/toolbox_template.go#L181-L217","documentation":"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.","triggerScenarios":"--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.","commonSituations":"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.","solutions":["Read the offending chunk in the error message and fix the YAML syntax at that point (indentation with spaces, quote strings containing : or #)","Ensure each document between --- separators is a YAML mapping (key: value), not a scalar or list","Quote values substituted into the template if they may contain YAML special characters","Temporarily drop --format-yAML to see the raw rendered output and diagnose what the substitution produced","Validate the rendered output with a linter such as yamllint"],"exampleFix":"// before (rendered doc)\nmetadata:\n\tname: my-cluster\n\n// after\nmetadata:\n  name: my-cluster","handlingStrategy":"validation","validationCode":"for _, doc := range strings.Split(rendered, \"---\\n\") {\n    var m map[string]interface{}\n    if err := yaml.Unmarshal([]byte(doc), &m); err != nil {\n        return fmt.Errorf(\"rendered chunk is not a YAML mapping: %v\", err)\n    }\n}","typeGuard":"func isYAMLMapping(doc string) bool {\n    var m map[string]interface{}\n    return yaml.Unmarshal([]byte(doc), &m) == nil\n}","tryCatchPattern":"if err := yaml.Unmarshal([]byte(doc), &data); err != nil {\n    return fmt.Errorf(\"document %q is not a YAML mapping (check tabs, unquoted special chars): %w\", doc, err)\n}","preventionTips":["Always use spaces, never tabs, in templates and values","Quote substituted values that may contain : or #","Ensure every --- separated section renders to a key/value mapping","Run yamllint on rendered output in CI before applying it"],"tags":["kops","yaml","templates","parsing"],"backgroundTag":"invalid-yaml-document","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}