合并YAML数组,忽略YAML中的其他字段

合并YAML数组,忽略YAML中的其他字段,yaml,yq,Yaml,Yq,我正在寻找一种方法来合并来自两个独立YAML的数组(将一个附加到另一个)。但是,YAML有一个用{}包装的字段(不在数组中),要用运行时值替换。i、 e # yaml a apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: namespace: default name: {clusterRoleName} rules: - apiGroups: [""] resourc

我正在寻找一种方法来合并来自两个独立YAML的数组(将一个附加到另一个)。但是,YAML有一个用
{}
包装的字段(不在数组中),要用运行时值替换。i、 e

# 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。

而使用像yq这样的专用yaml解析器将非常理想,使用awk可能是一种替代方法:

 awk 'NR==FNR && !/^[[:space:]]/ { tag=$1;next } tag=="rules:" && FNR==NR { map[idx++]=$0 } END { tag="" } NR!=FNR && !/^[[:space:]]/ { if (tag=="rules:") { for (i in map) { print map[i]}} tag=$1 } NR!=FNR { print }' yamlb yamla
说明:

awk 'NR==FNR && !/^[[:space:]]/ { # Processing yamlb (NR==FNR) and there there are spaces at the beginning of the line
         tag=$1; # Set the variable tag to the first space delimited field
         next # Skip to the next file
        } 
     tag=="rules:" && FNR==NR { # Process where tag is "rules:" and we are processing yamlb
          map[idx++]=$0 # Put the line in an array map with in incrementing index
        } 
     END { 
          tag="" # At the end of the file reset the variable tag
        } 
     NR!=FNR && !/^[[:space:]]/ { # Process yamla where there are no spaces at the start of the line
          if (tag=="rules:") { 
            for (i in map) { 
               print map[i] # If tag is equal to rules: print the array map
            }
          } 
          tag=$1 # Set the variable tag to the first space delimited field.
         } 
      NR!=FNR { 
          print # Print lines when we are processing yamla
         }' yamlb yamla

 

你能升级到更新的版本吗?V4中有很多新功能,升级可能是一种选择,尽管这种特定用途是大型项目的一部分,因此理想的解决方案是找到一个与现有版本兼容的解决方案。如果有一个干净的方式在v4虽然会感兴趣-将检查它谢谢:)谢谢!这真的很酷-最终我能够使用sed和yq的组合来解决模板值问题,感谢您花时间写这篇文章,因为阅读和尝试都很有趣。:)