使用 python 或 sed 在 yaml 中的特定位置添加键值

问题描述 投票:0回答:1

我试图在values.yaml中的特定位置添加以下键值,sed没有多大帮助,因为它破坏了yaml的缩进。有没有其他方法可以解决这个问题。

例如: 示例 yaml

enter image description here

所需的yaml

enter image description here

python sed kubernetes-helm
1个回答
2
投票

使用

sed
,您可以在
livenessProbe
之前插入一段文本,如下所示:

sed -e '/livenessProbe/i\
volumes:\n- name: my-agent\n  persistentVolume:\n  claimName: my-agent
' sample.yaml

如果

sample.yaml

resources:
  limits:
    cpu: 500m
    memory: 2Gi
  requests:
    cpu: 100m
    memory: 512Mi
livenessProbe:
  httpGetPath: /heartbeat

结果将是:

resources:
  limits:
    cpu: 500m
    memory: 2Gi
  requests:
    cpu: 100m
    memory: 512Mi
volumes:
- name: my-agent
  persistentVolume:
  claimName: my-agent
livenessProbe:
  httpGetPath: /heartbeat

如果要添加的代码块位于 yaml 文件中

addons.yaml
:

sed -e "/livenessProbe/i\
$(cat addons.yaml | sed ':a;N;$!ba;s/\n/\\n/g')
" sample.yaml
© www.soinside.com 2019 - 2024. All rights reserved.