Marc
Marc

Reputation: 650

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?

I am using mikefarah/yq version v4.30.6.

Upvotes: 0

Views: 985

Answers (1)

pmf
pmf

Reputation: 36601

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"

Upvotes: 3

Related Questions