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

Update value in yaml using yq and returning the whole yaml content

spec:
  templates:
  - name: some_step1
    script:
      image: some_image
      imagePullPolicy: IfNotPresent
  - name: some_step2
    activeDeadlineSeconds: 300
    script:
      image: some_image
      imagePullPolicy: IfNotPresent
      env:
        - name: HOST
          value: "HOST"
        - name: CONTEXT
          value: "CONTEXT"

I would like to update the value for CONTEXT. The following command updates the value however it does not return the whole yaml:

yq '.spec.templates | map(select(.name == "some_step2")).[].script.env | map(select(.name == "CONTEXT").value = "CONTEXT1")' test.yaml 

The output of the command above:

- name: HOST
  value: "HOST"
- name: CONTEXT
  value: "CONTEXT1"

How can I make changes in the yq command above to update the value for CONTEXT and return the whole yaml?

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

>Solution :

Use parentheses around the LHS of the assignment to retain the context, i.e. (…) = ….

( .spec.templates[]
  | select(.name == "some_step2").script.env[]
  | select(.name == "CONTEXT").value
) = "CONTEXT1"
spec:
  templates:
    - name: some_step1
      script:
        image: some_image
        imagePullPolicy: IfNotPresent
    - name: some_step2
      activeDeadlineSeconds: 300
      script:
        image: some_image
        imagePullPolicy: IfNotPresent
        env:
          - name: HOST
            value: "HOST"
          - name: CONTEXT
            value: "CONTEXT1"
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