YAML Cheatsheet

YAML syntax reference: scalars, lists, mappings, anchors, multi-line strings, and gotchas

The YAML cheatsheet covers scalars, lists, mappings, multi-line strings, anchors and aliases, type coercion gotchas, and real-world examples from Kubernetes, GitHub Actions, and Docker Compose.

How to Use This YAML Cheatsheet

YAML (YAML Ain't Markup Language) is a human-friendly data serialization format widely used for configuration files in DevOps tools — Kubernetes, Ansible, GitHub Actions, Docker Compose, and more. Understanding YAML syntax prevents config bugs.

Indentation Rules

YAML uses spaces for indentation (never tabs). The number of spaces per level is flexible but must be consistent within a document. Two spaces per level is the standard convention. Misaligned indentation is the most common YAML error.

Type Coercion Traps

YAML infers types automatically: yes, no, on, off are booleans. Numbers without quotes become integers or floats. Always quote values when the type matters.

Frequently Asked Questions

Is this YAML cheatsheet free?

Yes, completely free. All examples are copyable with one click.

What is the difference between YAML and JSON?

YAML is a superset of JSON with more human-readable syntax. YAML uses indentation (no braces), supports comments (#), has multi-line string syntax, anchors/aliases for reuse, and type inference. JSON requires double quotes for all strings and doesn't support comments. YAML is commonly used for config files; JSON for APIs.

What are common YAML gotchas?

Type coercion traps: yes/no/on/off are parsed as booleans (not strings), so config values like 'on' for a key need quoting. Numbers with leading zeros may be treated as octal in older parsers. Indentation must use spaces (never tabs). Empty values may be null. Strings with colons or special characters need quoting.

How do YAML anchors and aliases work?

Anchors (&name) mark a node for reuse. Aliases (*name) reference the anchored node, inserting the same value. Merge keys (<<: *anchor) merge a mapping's contents into another mapping. This is useful for DRY config: define common settings once and reference them in multiple places.

What is the difference between | and > for multi-line strings?

| (literal block): preserves newlines as-is, useful for scripts or multi-paragraph text. > (folded block): folds newlines into spaces (except double newlines), useful for long descriptions that should wrap. Both strip the final newline by default. Add - suffix (|- or >-) to strip trailing newline, + suffix (|+ or >+) to keep it.