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

如何使用yq替换YAML多行值中指定容器的image_tag?

使用yq修改YAML多行块字符串中的指定字段

解决命令

针对你的需求,使用mikefarah/yq(v4及以上版本)可通过以下命令实现:

yq eval '.values = (.values | fromyaml | .containerApps[] |= (select(.name == "app-frontend").image_tag = "yyyyyy") | toyaml | indent(2))' your-input.yaml

命令说明

  • .values | fromyaml:把values字段的多行块字符串解析为YAML对象,支持结构化修改
  • .containerApps[] |= (select(.name == "app-frontend").image_tag = "yyyyyy"):遍历containerApps数组,筛选出name为app-frontend的条目,修改其image_tag值为yyyyyy
  • toyaml:将修改后的YAML对象重新转换为字符串格式
  • indent(2):为转换后的字符串添加2个空格缩进,匹配原文件中块字符串的格式
  • .values = (...):把处理后的字符串重新赋值给values字段,替换原内容

验证效果

执行命令后,输出的YAML文件会和你预期的结果完全一致:

values: |
  nameOverride: my-service
  fullnameOverride: ""
  namespace: my-ns

  containerApps:
    - name: app-frontend
      image_tag: yyyyyy
    - name: app-backend
      image_tag: xxxxxxx

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 00:45:40