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

Replace an empty map with map data from a variable in yq

Using mikefarah/yq (4.25.3) I am trying to replace an empty map in a yaml file with a map stored in a string.

This is the map data:

RESOURCES=$(cat <<EOF
limits:
  cpu: 4000m
  memory: 3600Mi
requests:
  cpu: 500m
  memory: 900Mi
EOF
)

And this is what I am trying to execute:

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 ".cluster.resources = \"${RESOURCES}\"" values.yaml

As a result I get a multiline string (instead of a map):

resources: |-
    limits:
      cpu: 4000m
      memory: 3600Mi
    requests:
      cpu: 500m
      memory: 900Mi

How do I insert a map instead?

resources:
  limits:
    cpu: 4000m
    memory: 3600Mi
  requests:
    cpu: 500m
    memory: 900Mi

>Solution :

Use the env() operator to load environment variable into yq:

RESOURCES=$(cat <<EOF
limits:
  cpu: 4000m
  memory: 3600Mi
requests:
  cpu: 500m
  memory: 900Mi
EOF
)  yq --null-input ".cluster.resources = env(RESOURCES)"

Will produce:

cluster:
  resources:
    limits:
      cpu: 4000m
      memory: 3600Mi
    requests:
      cpu: 500m
      memory: 900Mi
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