YAML Reference
Free reference guide: YAML Reference
About YAML Reference
The YAML Reference is a detailed, searchable guide to YAML (YAML Ain't Markup Language), the human-readable data serialization format widely used in configuration files, CI/CD pipelines, Kubernetes manifests, Docker Compose files, and Ansible playbooks. It covers all core syntax elements from basic key-value pairs and comments to advanced features like anchors, aliases, merge keys, and complex mapping keys, with clear examples for each construct.
This reference organizes YAML syntax into six well-defined categories: Basic Syntax (key-value pairs, comments, indentation rules, document separators), Scalars (null, boolean, number, string, and date/time types), Sequences (block-style lists, inline flow-style arrays, nested multi-dimensional arrays, arrays of objects), Mappings (nested objects, inline flow-style objects, complex keys, merge keys), Anchors (anchor definitions, alias references, data reuse patterns), and Multiline (literal blocks, folded blocks, strip and keep modifiers).
Whether you are writing Kubernetes deployment manifests, configuring GitHub Actions workflows, defining Docker Compose stacks, or building Ansible automation playbooks, this reference provides the precise syntax details you need. Each entry includes a syntax pattern, clear description, and a copy-ready example demonstrating real-world usage. The tab-not-allowed indentation rule, the distinction between literal (|) and folded (>) block scalars, and the powerful anchor/alias system for DRY configurations are all thoroughly explained.
Key Features
- Complete scalar type reference including null (~), boolean, integer (hex, octal), float, and ISO 8601 date/time formats
- Block-style and flow-style syntax examples for both sequences and mappings with nested structures
- Anchor (&) and alias (*) patterns for eliminating repetition in configuration files with merge key (<<) support
- Multiline string handling with literal block (|), folded block (>), strip (-), and keep (+) modifiers explained
- Document separator (---) and end marker (...) usage for multi-document YAML streams
- Complex key syntax using ? for keys that are sequences, mappings, or multiline strings
- Type coercion examples using !!str, !!int tags to force specific data types in ambiguous cases
- Category-based filtering across Basic Syntax, Scalars, Sequences, Mappings, Anchors, and Multiline sections
Frequently Asked Questions
What is the difference between literal block (|) and folded block (>) in YAML?
The literal block scalar (|) preserves all newlines exactly as written, making it ideal for shell scripts, code blocks, or preformatted text embedded in YAML. The folded block scalar (>) replaces single newlines with spaces, joining lines into paragraphs, while double newlines (blank lines) are preserved as actual line breaks. This makes folded blocks useful for long descriptions or documentation strings that should wrap naturally.
How do YAML anchors and aliases work for reducing duplication?
Anchors (&name) mark a value for reuse, and aliases (*name) reference that stored value elsewhere in the document. Combined with the merge key (<<: *name), you can define default settings once and inherit them in multiple places, overriding only the fields that differ. For example, you can anchor database defaults and merge them into development, staging, and production configs, changing just the host and credentials per environment.
Why does YAML not allow tabs for indentation?
YAML strictly requires spaces for indentation because tab width is not standardized across editors and terminals (it can be 2, 4, or 8 spaces). Mixing tabs and spaces would cause inconsistent parsing, where a file that looks correctly indented in one editor breaks in another. Using spaces only ensures that the visual structure always matches the logical structure, preventing subtle hierarchy errors in configuration files.
How are null values represented in YAML?
YAML supports three ways to express null: the explicit keyword null, the tilde shorthand ~, and an empty value (a key with no value after the colon). All three parse to the same null/nil/None value in the target programming language. The empty-value form is commonly used for optional configuration fields that should default to nothing, while explicit null is preferred for clarity in complex documents.
What is the difference between block style and flow style in YAML?
Block style uses indentation to denote structure, with items on separate lines (e.g., sequences use "- item" and mappings use "key: value" on individual lines). Flow style uses JSON-like inline notation with brackets for sequences ([a, b, c]) and braces for mappings ({x: 1, y: 2}). Block style is more readable for complex, nested structures, while flow style is concise for short, simple collections that fit on one line.
How does YAML handle string quoting and special characters?
Plain (unquoted) strings work for most values but fail when the value contains special YAML characters like colons, brackets, or leading special characters. Single quotes preserve all characters literally except that single quotes are escaped by doubling them. Double quotes support escape sequences like \n for newlines, \t for tabs, and \x or \u for Unicode. Use !!str to force a numeric-looking value to be treated as a string.
How do multi-document YAML streams work with --- and ... markers?
The --- marker begins a new YAML document within a single file, allowing multiple independent documents to be concatenated. The ... marker optionally signals the end of a document. This is commonly used in Kubernetes, where a single file contains multiple resource definitions separated by ---. Parsers process each document independently, and a missing --- at the start of the first document is implicitly assumed.
What number formats does YAML support beyond standard decimal?
YAML supports decimal integers (42), negative integers (-17), floating-point numbers (3.14), scientific notation (1.0e+5), hexadecimal with 0x prefix (0xFF = 255), and octal with 0o prefix (0o77 = 63). Underscores can be used as visual separators in large numbers (1_000_000). Special float values .inf, -.inf, and .nan are also supported for representing infinity and not-a-number.