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

如何使用yq将template2.yaml的env数组添加到template1.yaml指定位置?

使用yq合并两个YAML文件的env数组

针对主流的mikefarah/yq v4+版本,以下是实现需求的具体操作:

核心命令

执行这条命令,就能将template2.yaml中的env数组追加到template1.yaml指定位置的env数组中:

yq eval '.spec.template.temp.vars[] |= (select(.name == "first").env += load("template2.yaml").env)' template1.yaml

命令拆解

  • .spec.template.temp.vars[]:遍历vars数组的每一个元素
  • select(.name == "first"):筛选出name字段为first的项
  • .env += load("template2.yaml").env:把template2.yaml里的env数组,追加到筛选出的项的env数组中
  • 整个表达式通过|=实现对原数据的修改

执行后的结果

template1.yaml会被更新为:

spec:
  template:
    temp:
      vars:
        - name: first
          env:
            - name: "first"
              value: 1
            - name: "two"
              value: 2

进阶:直接修改原文件

如果不需要预览结果,想直接修改template1.yaml,添加-i参数即可:

yq eval -i '.spec.template.temp.vars[] |= (select(.name == "first").env += load("template2.yaml").env)' template1.yaml

版本说明

确保你的yq是mikefarah/yq v4及以上版本,旧版本(比如v3或kislyuk/yq)语法完全不同,可以用yq --version检查当前版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 17:24:27