Rad4
Rad4

Reputation: 2374

Modifying yaml array in bash

I have a file with below content,

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

I want to append this file content as shown below with eks ,iammappings as first two lines,

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

I tried yq merge.But It didnt work for me.Please let me know how to do this.

Upvotes: 0

Views: 1011

Answers (1)

bigbounty
bigbounty

Reputation: 17358

There is a specific tool for parsing yaml in bash i.e yq same as jq. Link - https://github.com/mikefarah/yq

You have to modify the source yaml file as follows

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

Else yq won't accept it as proper yaml file.

Next to get your job done use the following command

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

The above command uses prefix function and will replace in place. The contents of the file will be as follows

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

Upvotes: 1

Related Questions