Fix YAML Anchor & Alias Reference Errors
YAML anchors (&), aliases (*), and merge keys (<<) let you reuse content. But when they break, the error messages are cryptic. Here's how to fix each type.
&anchor— define a named anchor on a node*alias— reference (reuse) an anchor's value<<: *alias— merge keys from a mapping anchor into the current mapping
How Anchors, Aliases, and Merge Keys Work
The &defaults anchor saves the mapping. The *defaults alias reuses it. The <<: merge key copies all keys from the anchor into the current mapping. Keys defined after the merge override the merged values.
Common Errors and Fixes
Error 1: Unknown Anchor
The alias references an anchor that doesn't exist or hasn't been defined yet
Invalid
Valid
Rule: Anchors must be defined before they are referenced. YAML parsers read top-to-bottom.
Error 2: Scope Violation
An alias references an anchor defined in a different document or scope
Invalid (cross-document)
Valid (same document)
Rule: Anchors cannot cross --- document boundaries. Each document is a separate scope. If you need shared data across documents, repeat the anchor in each document or restructure into a single document.
Error 3: Indentation Mismatch in Merged Content
The merge key or alias is at the wrong indentation level
Invalid (wrong indent)
Valid (correct indent)
Rule: The merge key <<: must be at the same indentation level as other keys in the mapping. It's a key like any other.
Error 4: Merge Key with Non-Mapping Anchor
The merge key only works with mappings (objects), not sequences or scalars
Invalid (anchor is a list)
Valid (anchor is a mapping)
Rule: <<: only merges mappings. To reuse a sequence, use a plain alias *alias without the merge key.
Error 5: Parser Compatibility Issues
Some YAML parsers don't support merge keys or have different behavior
Best Practices for YAML Anchors
Frequently Asked Questions
What is the difference between *alias and <<: *alias?
*alias replaces the entire node with the anchor's value. <<: *alias merges the keys from the anchor's mapping into the current mapping, and you can override individual keys. Use *alias when you want an exact copy, use <<: when you want a copy with modifications.
Can I use multiple merge keys in one mapping?
Yes. You can merge from multiple anchors: <<: [*defaults, *overrides]. Later anchors override earlier ones. However, not all parsers support the list syntax. The single form <<: *defaults is more widely supported.
Why does my YAML anchor work locally but fail in CI/CD?
Some CI/CD platforms (like older GitHub Actions runners or certain Kubernetes versions) use YAML parsers that don't fully support merge keys. Test with the same parser version your CI uses. If merge keys fail, replace them with explicit key-value pairs or use a YAML preprocessor to resolve anchors before submitting.
Can I override a merged key?
Yes. Keys defined after the merge key override the merged values. For example: <<: *defaults followed by timeout: 60 will use 60 for timeout, not the value from defaults. This is the main advantage of merge keys over plain aliases.
Key Takeaways
&anchordefines,*aliasreferences,<<:merges keys.- Anchors must be defined before they are used — YAML reads top-to-bottom.
- Anchors cannot cross
---document boundaries. - Merge keys only work with mappings, not sequences or scalars.
- Use our YAML Validator to catch anchor errors before deploying.