You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python为已有YAML文件添加新标签值

Python操作YAML:增删改标签值的正确方法

问题场景

你需要给YAML文件中的组件添加image.tag标签,基础YAML内容如下:

infra:
  etcd:
    container:
      replica_count: 3
      resource:
        limit_memory: 1000Mi
        limit_cpu: 1000m
        requests_memory: 1000Mi
        requests_cpu: 1000m
    volume:
      storageClaim: 5Gi
      storageCapacity: 5Gi
  kafka:
    container:
      replica_count: 3
      resource:
        limit_memory: 2000Mi
        limit_cpu: 1000m
        requests_memory: 2000Mi
        requests_cpu: 1000m
    volume:
      storageClaim: 10Gi
      storageCapacity: 10Gi        
  zk:
    container:
      replica_count: 3
      resource:
        limit_memory: 500Mi
        limit_cpu: 1000m
        requests_memory: 500Mi
        requests_cpu: 1000m
    volume:
      storageClaim: 10Gi
      storageCapacity: 10Gi

期望更新后的YAML为:

infra:
  etcd:
    container:
      image:
        tag: etcd-21.3.4
      replica_count: 3
      resource:
        limit_memory: 1000Mi
        limit_cpu: 1000m
        requests_memory: 1000Mi
        requests_cpu: 1000m
    volume:
      storageClaim: 5Gi
      storageCapacity: 5Gi
  kafka:
    container:
      image:
        tag: kafka-21.3.4
      replica_count: 3
      resource:
        limit_memory: 2000Mi
        limit_cpu: 1000m
        requests_memory: 2000Mi
        requests_cpu: 1000m
    volume:
      storageClaim: 10Gi
      storageCapacity: 10Gi        
  zk:
    container:
      image:
        tag: zk-21.3.4
      replica_count: 3
      resource:
        limit_memory: 500Mi
        limit_cpu: 1000m
        requests_memory: 500Mi
        requests_cpu: 1000m
    volume:
      storageClaim: 10Gi
      storageCapacity: 10Gi

但你编写的代码会丢失原有数据,错误代码如下:

import yaml

# Read the YAML file
with open ('in.yaml', 'r') as read_file:
    contents = yaml.safe_load(read_file)
   
contents['infra']['etcd'] = 'Image' 

# Write the YAML file with sort_keys=False to retain same order
with open('in.yaml', 'w') as write_file:
    yaml.dump(contents, write_file, sort_keys=False)

错误原因

你代码中contents['infra']['etcd'] = 'Image'是直接将etcd节点的全部内容替换成了字符串'Image',而非在原有结构里新增子节点,这就导致原有数据被完全覆盖。

正确操作方法

1. 添加新标签值(批量给组件加image.tag)

通过遍历infra下的所有组件,在每个组件的container节点中新增image.tag,不会覆盖原有数据:

import yaml

# 读取YAML文件
with open('in.yaml', 'r') as read_file:
    contents = yaml.safe_load(read_file)

# 遍历每个组件,添加对应的image.tag
for comp_name, comp_data in contents['infra'].items():
    # 在container字典中新增image子节点
    comp_data['container']['image'] = {
        'tag': f"{comp_name}-21.3.4"
    }

# 写入文件,保留原有键的顺序,使用块格式输出
with open('in.yaml', 'w') as write_file:
    yaml.dump(contents, write_file, sort_keys=False, default_flow_style=False)

2. 更新已有标签值

直接定位到目标标签,修改其值即可:

# 修改etcd的副本数
contents['infra']['etcd']['container']['replica_count'] = 4

# 更新kafka的镜像标签
contents['infra']['kafka']['container']['image']['tag'] = 'kafka-22.1.0'

3. 删除标签值

使用del语句删除指定标签:

# 删除zk节点下的storageCapacity标签
del contents['infra']['zk']['volume']['storageCapacity']

内容的提问来源于stack exchange,提问作者samir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 09:36:09