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

Is there a way to search through a yaml file for any instance of a key

I am currently trying to solve a problem with yq where we are updating the values file of a helm chart with an automated process. Most values files have the image tag in the following format:

image:
  repository: repo-name
  tag: 0.0.1

which we were updating with:

yq -i e '.image.tag = env(TAG)' chart-name/values.yaml

however not all charts follow the same format e.g. some have:

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

controller:
  image:
    repository: repo-name
    tag: 0.0.1

I want to be able to update the value of tag where ever it is nested within the yaml file if that is possible with yq or an alternative if it is possible.

>Solution :

Assuming you are using mikefarah/yq, you can use the recursive descent operator to update any key matching the name tag

yq '( .. |  select(has("tag")).tag ) |= env(TAG)' yaml

Unfortunately, the path operator is not efficient enough (unlike in jq) to get the paths of all root to leaf paths and update its value.

Note: If you are using yq version 4.18.1 or beyond, the eval flag e is no longer needed as it has been made the default action.

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