0

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.

4
  • Try: cat file2 >> file1 Commented Jul 14, 2020 at 22:25
  • But I need to add eks: ,iammappings as parents yaml tags to the array Commented Jul 14, 2020 at 22:38
  • FYI, all JSON files are also valid YAML files. Even if your input is formatted in a way that's conventional YAML, it'll work perfectly fine if your output is just JSON -- which means you have a much wider array of tools that can write it. Commented Jul 14, 2020 at 23:10
  • ...and are you sure your stated output is right? The way you state it, groups is the key in a dictionary whose value is null, and system:bootstrappers and system:nodes are not inside groups, but instead are separate items in the iammappings list. Commented Jul 14, 2020 at 23:17

1 Answer 1

1

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}}

Sign up to request clarification or add additional context in comments.

2 Comments

Incidentally, the OP already knows about yq -- they asked a question about its use just this morning.
@CharlesDuffy He didn't try yq prefix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.