Reusing YAML Blocks with Anchors, Aliases, and Merge Keys
Learn how YAML anchors, aliases, and the merge key let you define configuration once and reuse it safely, with a concrete example and verification steps.
28 Apr 2026, 09:33 UTC

Problem: Repeating the same configuration blocks
When a service definition, a set of environment variables, or a list of defaults appears in multiple places inside a YAML file, copying and pasting becomes error‑prone. A change in one copy must be mirrored everywhere else, which is tedious and risky.
Thesis: Anchors, aliases, and the merge key let you define a block once and reuse it safely
YAML 1.2 provides two core mechanisms:
- Anchor (&) – labels a node so it can be referenced later.
- Alias (*) – inserts the anchored node’s contents at another location.
The merge key (<<) is a widely‑supported convention (not part of the core spec) that lets an alias’s mapping be overlaid onto another map, giving a simple inheritance‑like behavior.
Worked example: Shared service defaults
Suppose you have several micro‑services that share the same logging configuration and resource limits. Instead of repeating those blocks, define them once with an anchor and reuse them with aliases and a merge key.
# services.yaml
defaults: &default-logging
level: info
format: json
retention: 7d
resources: &default-resources
cpu: "500m"
memory: "256Mi"
services:
web:
<<: *default-logging
<<: *default-resources
port: 8080
api:
<<: *default-logging
<<: *default-resources
port: 8081
worker:
<<: *default-logging
<<: *default-resources
concurrency: 10
When a YAML parser loads this file, each service map contains the keys from both default-logging and default-resources as if they had been copied manually.
How to verify the result
- Create a file named
services.yamlwith the content above. - Load it with a YAML library that supports merge keys (e.g.,
ruamel.yamlin Python):import ruamel.yaml yaml = ruamel.yaml.YAML() with open('services.yaml') as f: data = yaml.load(f) print(data['services']['web']) - Check that the printed dictionary includes
level,format,retention,cpu,memory, andport. - Change the anchored value (e.g., set
level: debugunder&default-logging) and reload the file; all services should reflect the new level without further edits.
Trade‑offs and limitations
- Scope of anchors – Anchors are only valid within the YAML document where they are defined. Trying to reference an anchor from a separate document (even if concatenated in the same stream) will raise a parser error.
- Merge key portability – The merge key is not part of the YAML 1.2 core specification. Most modern parsers (ruamel.yaml, SnakeYAML with extensions, libyaml with patches) support it, but strict validators or older PyYAML installations will treat
<<as a regular key and produce unexpected results. If you need maximum portability, avoid the merge key and rely solely on aliases for whole‑node reuse. - Readability** – Over‑using anchors can make a file harder to follow for newcomers, especially when the alias appears far from its definition. Keep anchors close to where they are used or add a comment describing their purpose.
Actionable closing
Start by identifying a repeated block in your current YAML configuration. Replace the first occurrence with an anchor, then substitute the rest with aliases. If you need to overlay that block onto existing maps, add a merge key (<<). Verify the output with a parser that supports merge keys, and document any parser‑specific requirements in your project’s README. This gives you a single source of truth for shared settings while keeping the file declarative and easy to audit.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.