Seven YAML traps: indentation, colons, quotes
Most YAML errors are about shape, not values. Start with how many spaces you used, whether there is a space after the colon, and whether a tab slipped in.
Tabs are not allowed as indentation
The YAML specification allows spaces only for indentation. Paste a file that an editor aligned with tabs and you get 'tab cannot be used for indentation'.
Replacing tabs with two spaces fixes more cases than any other single edit.
a: 1
b: 2 # line 2, column 1: tab
=> a: 1
b: 2
A colon needs a space after it
The colon that separates a key from a value must be followed by a space. a:b is not key a with value b; it is the string "a:b".
A colon inside a value, as in a URL or an image tag, is fine because it has no space after it.
| Written | Read as |
|---|---|
| name: web | key name, value web |
| image: nginx:1.27 | inner colon is kept |
| note: a: b | error, a key appears in the value |
| note: "a: b" | one string |
Line up the keys of a list item
When a mapping follows a sequence dash, the remaining keys must line up with the first key's column. Two extra spaces make them the next item.
The safe habit is one space after the dash and every following key aligned with that value's column.
containers:
- name: web
image: nginx
- name: sidecar
image: busybox
Questions
How many spaces is the standard indentation?
There is no fixed number, only consistency within a level. Two spaces is the most common choice and what the Kubernetes examples use.
Why does the reported line differ from the line I need to fix?
YAML reads each line relative to where the previous indentation ended, so the cause is often on the line above the reported error.
Sources
Every example here can be pasted into the converter as it is.
Open the converter