背景: 现在配置文件使用 yaml 的非常多,例如 docker-compose、k8s、gitlab-ci 等等,很多使用配置文件里有很多重复内容,如果简单粗暴的使用 ctrl+c & ctrl+v 就不够“优雅了”。

重复的内容可使从参考标记星号( * ) 复制到锚点标记( & )。

引用&替换

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
---
receipt:     Oz-Ware Purchase Invoice
date:        2012-08-06
customer:
    given:   Dorothy
    family:  Gale
   
items:
    - part_no:   A4786
      descrip:   Water Bucket (Filled)
      price:     1.47
      quantity:  4

    - part_no:   E1628
      descrip:   High Heeled "Ruby" Slippers
      size:      8
      price:     133.7
      quantity:  1

bill-to:  &id001 # 起个名字
    street: | 
            123 Tornado Alley
            Suite 16
    city:   East Centerville
    state:  KS

ship-to:  *id001  # 使用引用的内容更新该节点

specialDelivery:  >
    Follow the Yellow Brick
    Road to the Emerald City.
    Pay no attention to the
    man behind the curtain.

引用&插入

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
version: "3"

x-redash-service: &redash-service # 给这个节点起个名字,方便使用
  image: redash/redash:8.0.0.b32245
  env_file: env_file
  restart: unless-stopped
  networks: 
    - overlay-network-internal

services:
  server:
    <<: *redash-service # 使用前边定义的名字,把节点信息插入当前节点,这样就达成了复用效果
    command: server
    environment:
      REDASH_WEB_WORKERS: 4
    networks: 
      - overlay-network-internal
    ports:
      - 5000:5000
  scheduler:
    <<: *redash-service
    command: scheduler
    environment:
      QUEUES: "celery"
      WORKERS_COUNT: 1
  scheduled_worker:
    <<: *redash-service
    command: worker
    environment:
      QUEUES: "scheduled_queries,schemas"
      WORKERS_COUNT: 1
  adhoc_worker:
    <<: *redash-service
    command: worker
    environment:
      QUEUES: "queries"
      WORKERS_COUNT: 2

networks: 
    overlay-network-internal:
        external: true

参考