kislyuk / yq

Command-line YAML, XML, TOML processor - jq wrapper for YAML/XML/TOML documents
https://kislyuk.github.io/yq/
Apache License 2.0
2.57k stars 82 forks source link

Question - How can I replace an element of a list? #94

Closed vikas027 closed 4 years ago

vikas027 commented 4 years ago

I am trying to replace rolearn in the below yaml with a value, I've read the docs but could not figure out how to replace rolearn here.

apiVersion: v1
kind: ConfigMap
metadata:
  name: aws-auth
  namespace: kube-system
data:
  mapRoles: |
    - rolearn: <ARN of instance role (not instance profile)>
      username: system:node:{{EC2PrivateDNSName}}
kislyuk commented 4 years ago

Try export my_new_role_arn=...; yq .data.mapRoles[0].roleArn=env.my_new_role_arn.

On Sun, May 3, 2020 at 10:57 AM Vikas Kumar notifications@github.com wrote:

I am trying to replace rolearn in the below yaml with a value, I've read the docs but could not figure out how to replace rolearn here.

apiVersion: v1kind: ConfigMapmetadata: name: aws-auth namespace: kube-systemdata: mapRoles: | - rolearn: <ARN of instance role (not instance profile)> username: system:node:{{EC2PrivateDNSName}}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kislyuk/yq/issues/94, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGSOPN2FFVXEHBRXF44V3DRPWV7TANCNFSM4MYHDLGA .

vikas027 commented 4 years ago

yq .data.mapRoles[0].roleArn=env.my_new_role_arn

Thanks, @kislyuk .

I think I had tried doing the same but faced below error

~/.../eks (dev) $ export new_arn="xxxx"
~/.../eks (dev) $ yq .data.mapRoles[0].rolearn=env.new_arn aws-auth-cm.yaml 
jq: error (at <stdin>:1): Cannot index string with number
~/.../eks (dev) $ 
kislyuk commented 4 years ago

Ah, that's because your mapRoles is double-serialized. Do you know if it's supposed to be that way? If it is, you can do something like export value=NEW_ARN; export inner=$(yq -r .data.mapRoles t.yml| yq .[0].rolearn=env.value); yq .data.mapRoles=env.inner t.yml where your input file is in t.yml.

vikas027 commented 4 years ago

Thanks, @kislyuk That worked like a charm but it is a bit of an overkill with yq I guess, I will stick to sed for this use case :)