Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

yq is not properly interpreting map from list of maps if it comes from env variable

I am trying to append new entries to the following yaml:

  destinations:
    - namespace: ns1
      server: https://kubernetes.default.svc

What I expect is:

  destinations:
    - namespace: ns1
      server: https://kubernetes.default.svc
    - namespace: ns2
      server: https://kubernetes.default.svc- 
    - namespace: ns3
      server: https://kubernetes.default.svc

and I can get is, if I do like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

yq -i '.spec.destinations += [{"namespace": "zi-test-customer", "server": "https://kubernetes.default.svc"},{"namespace": "zi-test-customer2", "server": "https://kubernetes.default.svc"}]' file.yaml

But if I take the list from env varible I get wrong list

What I am getting is:

  destinations:
    - namespace: ns1
      server: https://kubernetes.default.svc
    - {"namespace": "ns2", "server": "https://kubernetes.default.svc"}
    - {"namespace": "ns3", "server": "https://kubernetes.default.svc"}

If I do this

NS=[{"namespace": "ns2", "server": "https://kubernetes.default.svc"},{"namespace": "ns3", "server": "https://kubernetes.default.svc"}]
yq -i '.spec.destinations += env(NS)' file.yaml

How extract values from env varible properly?

>Solution :

What you see as "wrong result" is nothing but a different representation. You’re importing a list in JSON format, and its "style" is retained. But the actual data stored is the same in both your expected and your "wrong" result.

If you want to remove the style, mikefarah/yq provides the style operator. Remove it by setting it to an empty string:

NS='[{"namespace": "ns2", "server": "https://kubernetes.default.svc"},{"namespace": "ns3", "server": "https://kubernetes.default.svc"}]'
NS="$NS" yq .spec.destinations += env(NS) | ... style=""' file.yaml
spec:
  destinations:
    - namespace: ns1
      server: https://kubernetes.default.svc
    - namespace: ns2
      server: https://kubernetes.default.svc
    - namespace: ns3
      server: https://kubernetes.default.svc
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading