Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

YAML anchors, aliases, overrides, and extensions help reduce the repetition of data in your YAML files. These features of YAML are discussed in this guide to take you beyond the basics covered in the A YAML Syntax Reference guide.

YAML Anchors and Aliases

Suppose you use Docker Compose to specify a particular WordPress customization. Docker itself offers an example specification that, as of this writing, begins:

1
2
3
4
5
6
7
8
9
version: "3.9"

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
...

Docker’s documentation illustrates how to use a docker-compose.yml to create a basic blog backed by a data store with a volume mounted on /var/lib/mysql.

In a professional context, though, you don’t only need a backing data store for the WordPress instance, but multiple WordPress instances. It is common to define a production instance that supports end-users in their real-life WordPress activities, along with a test instance to verify the correctness of functionality before exposing end-users to it. One way to implement multiple definitions is simply to write them naively.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
version: "3.9"

services:
  production-db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
  MYSQL_ROOT_PASSWORD: somewordpress
  MYSQL_DATABASE: wordpress
  MYSQL_USER: wordpress
  MYSQL_PASSWORD: wordpress
      ...
  test-db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
    MYSQL_ROOT_PASSWORD: somewordpress
    MYSQL_DATABASE: wordpress
    MYSQL_USER: wordpress
    MYSQL_PASSWORD: wordpress

An anchor (&) and alias (*) abbreviate these definitions down to:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
version: "3.9"

services:
  production-db: &database-definition
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
  MYSQL_ROOT_PASSWORD: somewordpress
  MYSQL_DATABASE: wordpress
  MYSQL_USER: wordpress
  MYSQL_PASSWORD: wordpress
      ...
  test-db: *database-definition

In this example, the &database-definition is an anchor to which the *database-definition alias refers.

The alias abbreviates YAML content, compacting it down so it takes up fewer bytes in a file system. More importantly, human readers have less to take in and thus focus more effectively on the essentials of the definition. Moreover, these anchor-alias combinations can ease maintenance chores. Suppose MYSQL_USER needs to be updated from wordpress to special_wordpress_account: while naive YAML requires editing the MYSQL_USER in each of its uses–presumably the same as the number of databases in all environments–the rewritten YAML only needs an update to its one anchor. Each alias then properly receives the updated special_wordpress_account automatically. Fewer distinct values to copy-and-paste inevitably mean fewer opportunities for inadvertent error.

Aliases often shrink complex YAML specifications down to half or even a smaller fraction of their original sizes.

YAML Overrides

Sometimes segments of a YAML file share only part of their contents. The WordPress example might configure databases that are identical except that each instance has a distinct password. YAML’s overrides allow for this situation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
version: "3.9"

services:
  production-db: &database-definition
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment: &environment-definition
  MYSQL_ROOT_PASSWORD: somewordpress
  MYSQL_DATABASE: wordpress
  MYSQL_USER: wordpress
  MYSQL_PASSWORD: production-password
      ...
  test-db:
    <<: *database-definition
    environment:
        <<: *environment-definition
  MYSQL_PASSWORD: test-password
  ...

The << is a special override syntax that effectively allows for an alias whose individual values can be updated.

Economy of Expression

Anchors, aliases, and overrides provide brevity for your YAML configuration files. Any YAML written with these constructs can be expanded into a valid form that helps you keep your configuration files DRY. These features of the YAML language help keep your files compact and make them easier to understand and maintain.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.