input.yaml 中存储了一个动态属性,我想将其插入到现有的 yaml 文件中(命名为 original.yaml)。该属性有多行。
这两个文件看起来像: input.yaml:
- name: bob
spec: {}
original.yaml:
spec:
names:
- name: alice
spec: {}
我的目标是将 input.yaml 内容放在 original.yaml spec.names
下。
我尝试使用 yq 版本 4:
env=$(cat input.yaml)
yq eval '.spec.names + strenv(env)' original.yaml > result.yaml
我得到了什么:
spec:
names:
- name: alice
spec: {}
- |-
- name: bob
spec: {}
第 5 行中有一个不需要的 - |-
,我希望得到以下输出:
spec:
names:
- name: alice
spec: {}
- name: bob
spec: {}
任何建议将不胜感激。
答案 0 :(得分:2)
这个想法是对的,但您不应该使用 strenv()
函数,该函数将您的输入格式化为 YAML 字符串类型。您的 names
记录是一个地图数组,因此您需要保留原始类型,只需使用 env
因此,通过避免 cat
并使用 shell 输入重定向(应该适用于 bash/zsh/ksh),您可以这样做
item="$(< input.yaml)" yq e '.spec.names += env(item)' original.yaml