0

我有一个包含以下内容的文件,

- groups:
  - system:bootstrappers
  - system:nodes
  rolearn: arn:aws:iam::1234566:role/radeks-project-us-east-1-NodeInstanceRole
  username: system:node:{{EC2PrivateDNSName}}

我想附加这个文件内容,如下所示,前两行是 eks ,iammappings,

eks:
  iammappings:
  - groups:
  - system:bootstrappers
  - system:nodes
  rolearn: arn:aws:iam::1234566:role/radeks-project-us-east-1-NodeInstanceRole
  username: system:node:{{EC2PrivateDNSName}}

我尝试了 yq 合并。但它对我不起作用。请让我知道如何执行此操作。

4

1 回答 1

1

在 bash 中有一个用于解析 yaml 的特定工具,即yqjq. 链接 - https://github.com/mikefarah/yq

您必须按如下方式修改源 yaml 文件

- groups:
    - system:bootstrappers
    - system:nodes
    - rolearn: arn:aws:iam::1234566:role/radeks-project-us-east-1-NodeInstanceRole
    - username: system:node:{{EC2PrivateDNSName}}

否则yq不会接受它作为正确的 yaml 文件。

接下来要完成工作,请使用以下命令

yq p -i file.yaml 'eks.iammappings'

上面的命令使用prefix函数并将替换到位。文件内容如下

eks:
  iammappings:
    - groups:
        - system:bootstrappers
        - system:nodes
        - rolearn: arn:aws:iam::1234566:role/radeks-project-us-east-1-NodeInstanceRole
        - username: system:node:{{EC2PrivateDNSName}}

于 2020-07-14T23:03:47.643 回答