【问题标题】:Merge cloud yaml AWS cloud formation template合并云 yaml AWS 云形成模板
【发布时间】:2020-07-22 17:52:11
【问题描述】:

我有多个文件,其中包含我的云形成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, ""]]

应用程序.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 让节点保持简单?并且在dump的时候还能再次输出吗?

我试过了

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

但是当我转储文件时,我会在我的 yaml 中为每个 !ref 获取 SequenceNode 对象

例如:

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:

【问题讨论】:

    标签: python yaml amazon-cloudformation


    【解决方案1】:

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

    它似乎可以使用RoundTripruamel

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

    【讨论】:

      猜你喜欢
      • 2020-04-14
      • 2021-08-24
      • 2020-01-02
      • 2017-05-21
      • 2020-01-08
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      相关资源
      最近更新 更多