Use this when reusing YAML blocks with anchors.
Anchors
YAML anchors (&name) and aliases (*name) reuse a block elsewhere in the same file, and merge keys (<<: *name) combine it with extra fields, reducing repetition in CI and Compose files.
1. Anchors (&) and aliases (*) reuse YAML blocks.
2. Merge keys (<<:) are YAML 1.1 — not supported everywhere.
3. Can obscure config — use sparingly.
4. Watch alias bombs (billion laughs) — safe loaders limit them.
5. Expand to verify.
&anchor → *alias → reused content
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
Parse the same fixture with the production parser/library and inspect the resulting mapping/sequence values, not just text formatting. Confirm aliases reference the intended anchored content, test an undefined alias and any merge-key pattern used by the project, and schema-validate the composed data if a schema exists. Compare behavior after parser upgrades.
Step 2 Common mistakes Narrow the fault
- Anchors/aliases unsupported by the consuming tool.
Step 3 Where it fails Narrow the fault
Separate YAML lexical/syntax parsing, anchor/alias composition, application-specific merge behavior, schema validation and downstream automation semantics. An unidentified alias is a YAML composition error, while a valid alias that yields the wrong business configuration is a higher-layer modeling error. Do not “fix” by duplicating text until parser and composed structure are understood.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- &anchor defines a block; *alias reuses it.
- <<: *alias merges a mapping.
- Anchors only work within one file.
yq 'explode(.)' file.yaml— expand anchors/aliases- Anchor (&) and alias (*) usage — reuse
- Merge keys (<<) — support varies
ReferenceWhy it matters1 section · depth on demand
In depth
YAML anchors (&name) mark a node, aliases (*name) reuse it, and the merge key (<<: *name) copies a mapping's keys into another mapping. They reduce repetition within one YAML file.
defaults: &access_defaults
mode: access
portfast: true
bpduguard: true
interfaces:
Gi1/0/1:
<<: *access_defaults
vlan: 20
Gi1/0/2:
<<: *access_defaults
vlan: 30
Caveats: anchors only work within the same file; the merge key is a YAML 1.1 feature not supported by every parser (some YAML 1.2 tools reject it); and heavy use makes files hard to read. In Ansible and Kubernetes, group variables, Helm values or Kustomize are often clearer than anchors.
ReferenceImportant terms & boundaries1 section · depth on demand
Mechanics
YAML anchors label a node in the serialization and aliases refer to a previously anchored node. The specification treats anchor names as serialization details; consumers work with the composed representation, not durable anchor identity. Reuse can reduce repetition, but application-specific merge-key behavior and parser limits are not the same thing as core alias semantics. Different tooling may expand, preserve or restrict aliases differently.
ReferenceConfiguration & state1 section · depth on demand
Lifecycle
Keep YAML syntax compatible with the parsers actually used by CI, configuration management and application runtimes. Avoid using anchors as hidden cross-file or business identifiers, and document when a repository relies on non-core merge behavior. Large or recursively expanded alias graphs can create parser/resource risks, so set appropriate parser limits and test dependency upgrades.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in YAML > Anchors. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://yaml.org/spec/1.2.2/ · confidence 94%Quick lookup tables
YAML → Anchors · operational reference
Context-specific ownership, verification, output and failure-boundary lookup for YAML → Anchors.
| Layer | YAML → Anchors operational reference |
|---|---|
| Runtime/ownership | YAML 1.2.2 anchors and aliases · identify controller/parser, durable intent, identity and target ownership before mutation |
| Persistent intent | Versioned definition/configuration with explicit dependencies, scope and credential boundary |
| Verification | Parse a fixture containing &anchor and *alias with the actual automation parser, inspect the resulting native structure, then round-trip or schema-validate where supported. |
| Expected interpretation | Aliases resolve to the intended previously anchored node, merge/application semantics are parser-compatible, and the automation does not rely on anchor names as durable business data. |
| Recovery boundary | Preserve correlation IDs + pre-state, repair first failing boundary, then verify the target independently |
Deep reference
Healthy vs suspicious evidence
Output interpretation
Capture parser/library version, source file, parse result or structured error, alias/anchor locations relevant to the failure, and the composed values consumed by automation. A pretty-printed YAML round-trip may rename or discard anchors without changing represented data; that alone is not a semantic failure. Preserve original source for diagnostics.