gpt4 book ai didi

python - 合并cloud yaml AWS云形成模板

转载 作者:行者123 更新时间:2023-12-03 07:42:53 25 4
gpt4 key购买 nike

我有多个文件,其中包含我的云形成 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: could not determine a constructor for the tag '!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:

最佳答案

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

看起来它可以使用RoundTripruamel

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

关于python - 合并cloud yaml AWS云形成模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61132534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com