YAML Anchors & Aliases

Define a value once with & and reuse it with * — less duplication, fewer mistakes.

Validate YAML that uses anchors and aliases with the YAML Validator — YAML Validator →

The idea

YAML lets you anchor a node with &name and alias it later with *name. Both refer to the same data, so you write it once.

Basic example

defaults: &base
  retry: 3
  timeout: 30

service_a:
  <<: *base
  name: api

service_b:
  <<: *base
  name: worker

Here &base anchors the mapping, and *base reuses it. The << merge key pulls the anchored fields in.

Why use them

Common pitfalls

Validate before you ship

Anchors are powerful but easy to typo. Paste your YAML into the YAML Validator to confirm every alias resolves before committing.

Related guides