BudiBadu Logo
Samplebadu

YAML by Example: Anchors

1.2

Define reusable chunks. Mark nodes for later reference.

Code

# Define an anchor with &
defaults: &base_settings
  timeout: 30
  retries: 3
  protocol: https

# Use the anchor later (see Aliases example)
development:
  <<: *base_settings
  debug: true

production:
  <<: *base_settings
  timeout: 60

Explanation

Anchors let you mark a node with a name using the & symbol. This creates a reference point that can be reused later with aliases. Anchors help avoid repetition in configuration files where multiple sections share common settings.

The anchor name can be any identifier you choose. Make it descriptive like &database_defaults or &production_config. The anchor must be defined before you reference it with an alias.

Code Breakdown

2
&base_settings creates an anchor named "base_settings" for this block.