我正在尝试用我自己的变量编辑YAML文件
这是我正在编辑的YAML模板:
Mappings:
PrivateLink:
EndPointService:
EndPointService: hostname1
PrincipalName: ""
NLBName:
NLBName:
Name: hostname1
TargetName:
hostname1-22:
Name: hostname1-22
VpcId: vpc-123
Id: "i-123"
我的目标是循环列出我拥有的端口号,并使用它们创建新的目标组
例如,如果我的端口列表为[22, 80, 443]
,则输出YAML如下所示:
Mappings:
PrivateLink:
EndPointService:
EndPointService: hostname1
PrincipalName: ""
NLBName:
NLBName:
Name: hostname1
TargetName:
#Editing is done here
hostname1-22:
Name: hostname1-22
VpcId: vpc-123
Id: "i-123"
hostname1-80:
Name: hostname1-80
VpcId: vpc-123
Id: "i-123"
hostname1-443:
Name: hostname1-443
VpcId: vpc-123
Id: "i-123"
对于我的函数,如下所示:
def editEndpointServiceTemplate(endpoint_service_template_path):
yaml = YAML()
yaml.indent(mapping=3)
ports_list = ast.literal_eval(configParser.get("Ports", "Ports"))
#Load yaml file
with open(endpoint_service_template_path) as fp:
data = yaml.load(fp)
for i in range(0, len(ports_list)):
tg_name = service_name + "-" + str(ports_list[i])
data['Mappings']['TargetName'][tg_name] = None
data['Mappings']['TargetName'][tg_name]['Name'] = "test"
#Write new yaml file
with open(endpoint_service_template_path, 'w') as fp:
yaml.dump(data, fp)
我在这个错误上失败了:
data['Mappings']['TargetName'][tg_name]['Name'] = "test"
TypeError: 'str' object does not support item assignment
我想我无法访问我刚刚创建的密钥,但我不知道如何修复它
通过更改来修复它:
为此:
现在已经修好了。我想我必须把它设为dict对象
相关问题 更多 >
编程相关推荐