覆盖Yaml中的变量

时间:2019-01-23 14:15:08

标签: yaml

我有一个yml配置文件,如下所示:

default: &default
  aws_access_key: <%= ENV['AWS_ACCESS_KEY'] %>
  aws_secret_key: <%= ENV['AWS_SECRET_KEY'] %>
  region: <%= ENV['AWS_REGION'] %>
  aws_s3_bucket: <%= ENV['AWS_S3_BUCKET'] %>
  aws_s3_host: "https://s3-eu-west-1.amazonaws.com/"

development:
  <<: *default

我希望在development中使用所有默认值进行扩展,但要覆盖存储桶名称。我该如何实现?可能这是一个非常基本的问题,但我找不到任何相关的信息。

1 个答案:

答案 0 :(得分:-1)

如果可以使用python,那很简单:

import yaml

# read your first file
with open("basefile.yaml", 'r') as f:
    conf = yaml.load(f)

# read your second file
with open("devfile.yaml", 'r') as f:
    devconf = yaml.load(f)

# update the first dictionnary with the values of the second
conf.update(devconf)

# write it in a new file
with open("result.yaml", 'w+') as f:
    yaml.dump(conf, f)