合并云Yaml AWS云形成模板

时间:2020-04-10 00:59:53

标签: python yaml amazon-cloudformation

我有多个文件,其中包含我的云形成template.yaml文件。例如

base.yaml

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

Conditions:
  conFunctionInVPC: !And
    - !Not [!Equals [!Join ["", !Ref parFunctionSubnets], ""]]
    - !Not [!Equals [!Join ["", !Ref parFunctionSecurityGroups], ""]]
  conFunctionNotInVPC: !Not [!Condition conFunctionInVPC]
  conAwsXRaySdkLayer: !Not [!Equals [!Ref parAwsXRaySdkLayerArn, ""]]

application.yaml

Description: >
  test
  A short description of the function purpose

我要生成最终的Yaml文件。我尝试了一个简单的合并功能

with open('base.yaml') as f:
    base = yaml.load(f)
with open('application.yaml') as f:
    application = yaml.load(fp)
template = merge(base, template) # my own function not important here
yaml.dump(template, open('template.yaml', 'w'))

但是我得到了错误:

yaml.constructor.ConstructorError:无法确定标签'!And'的构造函数

如何告诉yaml将节点保持简单?并能在转储时再次输出?

我尝试过

yaml.add_multi_constructor('!', lambda loader, suffix, node: node)

但是当我转储文件时,我在Yaml中为每个SequenceNode获得了!ref对象

例如:

conFunctionInVPC: !!python/object:yaml.nodes.SequenceNode
    end_mark: !!python/object:yaml.error.Mark
      buffer: null
      column: 2
      index: 721
      line: 24
      name: template/template.yaml
      pointer: null
    flow_style: false
    start_mark: !!python/object:yaml.error.Mark
      buffer: null
      column: 20
      index: 581
      line: 21
      name: template/template.yaml
      pointer: null
    tag: '!And'
    value:

1 个答案:

答案 0 :(得分:0)

看着YAML Error: could not determine a constructor for the tag

似乎可以使用RoundTripruamel

template = load(f, Loader=RoundTripLoader)
yaml.dump(template, f, Dumper=RoundTripDumper)