4

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
DeclanG
  • 240
  • 2
  • 6
  • 21

2 Answers2

20

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".

Nadjib Mami
  • 5,736
  • 9
  • 37
  • 49
0stone0
  • 34,288
  • 4
  • 39
  • 64
6

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
Franxi Hidro
  • 505
  • 4
  • 18