The YAML format is used to define configuration data. Instead of relying on complex syntax, YAML expresses structure through indentation and layout.
YAML File Structure and Indentation Rules
Scalars
The simplest YAML structure is a key-value pair whose value is a scalar (i.e., an indivisible value). Scalar values must appear on the same line as their corresponding keys. A single space must follow the colon, before the value.
An unquoted value that does not match another scalar type (i.e., production) is treated as string.
Mappings
Values may be in the form of mappings (one or more key-value pairs). Indentation shows that nested key-value pairs belong to the parent key. Nested key-value pairs at the same level must use the same indentation.
Examples - Mappings
example 1
YAML
application:
name: "demo"
enabled: true
Explanation: The keyapplicationis a top-level entry. The keysnameand enabledare both indented by two spaces, indicating that they belong toapplication.
example 2
YAML
database:
host: db.example.com
port: 5432
An unquoted value that does not match another scalar type (i.e., db.example.com) is treated as a string.
COMMON ERROR
Indentation errors are the most common cause of YAML failures.
Sequences and Nested Structures
A sequence is a single value composed of an ordered collection of items, which may be a scalar, a mapping, or another sequence. This type of value can be written in two styles:
Block Style: Each sequence item (- ) appears on a new line starting with a hyphen and a space. All content indented more than the hyphen's indentation level belongs to that sequence item. Indentation of sibling sequence items must be consistent.
Example - Block Style *
example 1
YAML
server:
name: demo
ports:
- 80
- 443
The sequence is the value of ports. The items 80and 443 are items of that sequence.
Flat and nested sequences represent different structures, from which application behavior emerges.
Flow Style: Sequences are enclosed in square brackets and separated by commas ([value1, value2]). When a sequence item is a mapping, its key-value pairs are enclosed in curly braces and separated by commas ({ key1: value1, key2: value2 }).
# Evidence retention policy
evidence:
retention_days: 90 # Automatically purges evidence after retention period
Start and End Markers
YAML documents may include explicit start (---) and end (...) markers. Each document in a multi-document YAML file is treated as an independent YAML file, even though they share the same physical file.
Examples - Start and End Markers
Example 1
YAML
---
task: backup
target: database
schedule: daily
...
---
task: cleanup
target: logs
schedule: weekly
...
---
task: upgrade
target: api
scheduled_at: 2027-01-01T09:00:00 # in UTC
...
A document ends implicitly when a new --- marker appears or when the file ends. Therefore, ... markers may be omitted.
Anchors and Aliases
Anchors and aliases, together, reduce duplication by allowing reuse of configuration values. An anchor (&) defines a reusable value, and an alias (*) inserts the entire anchored value, regardless of whether it is a scalar, mapping, or sequence.
Explanation: The anchor &settingsis attached to a mapping composed of the key-value pairs timeout: 30and retries: 3, and the alias *settingsreuses that entire mapping.
Explanation: The anchor &ports is attached to a sequence composed of the items 80and443, and the alias *portsreuses that entire sequence.
FAQs
What are key-value pairs?
A YAML file is made of pairs. Each pair has a key and a value, with the value belonging to the key that precedes it. The key tells the system what the value represents, and the value provides the actual data associated with that key.
A key appears before the colon. The value appears after the colon.
Example
YAML
name: demo
Explanation: In this line, nameis the key, and demois the value.
Keys can group other key-value pairs, with the nested pairs collectively forming the value of the parent key.
Example
YAML
application:
name: demo
enabled: true
Explanation: application is the parent key. The value ofapplication is not a single scalar, but rather a group of nested key-value pairs.
What are scalars?
A scalar refers to a single, indivisible value. YAML supports the following scalar types:
String ("demoSvr")
Integer (443)
Float (1.5)
Boolean (true, false)
Null (null)
Quoted values are always treated as strings. Unquoted values that do not match another type are treated as strings.