yq replace value in manifest yaml
Asked Answered
I

2

10

I have a k8s manifest file for loadbalancer below and cannot for the life of me get the $ipaddress be replaced with value, I have got to to overwrite whole file or part of or even just leave blank. How can I replace only the $ipaddress like in below

Tried as example 2 below:

yq e '.spec|=select(.loadBalancerIP) .ports.port = "172.16.87.98"' manifest.yaml
yq e -i '(.spec|=select(.loadBalancerIP.$ipaddress) = "172.16.87.98"' manifest.yaml
  apiVersion: v1
    kind: Service
    metadata:
      name: my-lb-cluster
    spec:
      loadBalancerIP: $ipaddress
      ports:
        - name: ssl
          port: 8080
      selector:
        role: webserver
      sessionAffinity: None
      type: LoadBalancer
Irresolute answered 14/1, 2022 at 16:12 Comment(4)
Not sure which tag you wanted, but doesn't seem to be related to jqueryMorvin
Why the jq tag?Chaplain
apologies, trying to remove , added by mistakeIrresolute
Are there multiple entrys in the yaml?Chaplain
C
30

If the YAML is as simple as in your question, you can use:

yq e -i '.spec.loadBalancerIP = "172.16.87.98"' manifest.yaml

...to update manifest.yaml and set .loadBalancerIP inside .spec to "172.16.87.98".

Chaplain answered 14/1, 2022 at 16:22 Comment(2)
ah dude, thanks I was over complicating this thinking i needed a select to find thisIrresolute
latest version of yq from 4.18.1 sets eval/e to the default command, so we can remove it altogether. yq -i '.spec.loadBalancerIP = "172.16.87.98"' manifest.yamlScream
L
9

I know it's late but this can help if you want to pass the value from a variable.

export LB_IP=1.1.1.1

yq e -i '.spec.loadBalancerIP= env(LB_IP)' manifest.yaml
Layout answered 10/10, 2022 at 17:22 Comment(1)
Late but useful!Bare

© 2022 - 2024 — McMap. All rights reserved.