Using YAML Merge Key (<<) for Reusable Configurations
Learn how the YAML merge key lets you inherit mappings, reuse config blocks, and avoid duplication. A step‑by‑step example, limits, and common pitfalls are shown.
21 Apr 2026, 14:39 UTC

Why the merge key matters
When multiple configuration sections share a large set of common values, repeating the same keys is error‑prone and hard to maintain. YAML’s merge key (<<) lets you declare a block once and then “inherit” it in other mappings, keeping your files concise and consistent.
How the merge key works
The merge key is a mapping entry whose key is << and whose value is a reference to an anchored mapping. When a YAML parser processes the document, it expands the referenced mapping inline, just as if the key/value pairs had been written directly in the target mapping. If the target mapping later defines a key that already exists in the merged source, the target value takes precedence.
Worked example
# Define a reusable block and give it an anchor
# The anchor name can be any valid identifier after &
defaults: &default
host: example.com
port: 80
# Inherit the block and override specific keys
# The merge key appears first, but its position relative to other keys determines precedence
# Keys defined after the merge key override the merged values
development:
<<: *default
port: 8080
debug: true
production:
<<: *default
host: prod.example.com
# No override for port, so it stays 80
When parsed, the development mapping resolves to:
{host: "example.com", port: 8080, debug: true}
And production resolves to:
{host: "prod.example.com", port: 80}
Verification checklist
- Run the YAML through a parser that declares merge support, such as
ruamel.yamlin Python:import ruamel.yaml yaml = ruamel.yaml.YAML(typ="safe") with open("config.yaml") as f: data = yaml.load(f) print(data["development"]) # Expect host=example.com, port=8080, debug=True - Parse the same file with a parser that does not support merges (e.g., older
PyYAMLwithout theLoader=yaml.FullLoaderflag). Observe that thedevelopmentmapping lacks the merged keys or raises an error, confirming that merge support is required. - Test precedence by adding two merge keys in sequence:
The finalmulti: <<: *default <<: *override port: 9090portwill be 9090 because the last merge key applied is*override.
Limits and pitfalls
- YAML version: Merge key is part of YAML 1.1. Some YAML 1.2 parsers ignore it unless explicitly enabled, so test your runtime library.
- Only mappings: The value of
<<must be a mapping (or a sequence of mappings). Sequences or scalars cause type errors in strict parsers. - Precedence rules: Keys defined after the merge key override merged values. If you place a duplicate key before the merge, the merged value will win, which can be confusing.
- Readability: Excessive use of merge keys can make a file harder to read, especially for new team members. Document the pattern in your style guide.
- Parser compatibility: Some libraries (e.g., older
PyYAML) silently drop merge keys. Always verify with the target environment.
Common mistakes to avoid
- Forgetting to anchor the source mapping:
defaults: &defaultis required; otherwise*defaultwill be unresolved. - Using duplicate keys before the merge key: the merged values will overwrite them, potentially breaking expected defaults.
- Attempting to merge a sequence:
list: [1, 2]cannot be merged; you must merge mappings only. - Relying on merge keys for large, complex structures without testing in all target parsers; a missing merge can silently change runtime behavior.
When to use it
Merge keys shine when:
- You have a base configuration that many environments extend.
- Maintaining a single source of truth for shared settings reduces duplication.
- Your team uses a YAML library that guarantees merge support.
When you need to combine lists or more complex inheritance, consider alternative patterns such as include directives or templating tools.
Final take‑away
The YAML merge key (<<) is a lightweight tool for reusing configuration blocks. By anchoring a mapping and merging it into other mappings, you keep files DRY and maintain consistent defaults. Just remember to verify parser support, respect precedence rules, and keep your YAML readable.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.