我正在寻找一种方法来合并来自两个独立 YAML 的数组(将一个附加到另一个)。然而,YAML 有一个用运行时值替换的字段(不在数组中){}
。IE
# yaml a
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
namespace: default
name: {clusterRoleName}
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
# yaml b
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
namespace: default
name: {clusterRoleName}
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "watch", "list"]
# desired
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
namespace: default
name: {clusterRoleName}
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "watch", "list"]
为此,我使用的是 yq 版本 2.14。我试过yq merge -a=append a.yaml b.yaml
了,它可以根据需要处理规则数组,但将name: {clusterRoleName}
其视为 JSON 和输出:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: {ClusterRoleName: ''}
...
有没有办法只合并一个字段,或者忽略特定的键或值类型?如果有人能够建议一种替代方法,我也不会为此使用 yq 。