title: How to update value of a yaml key value pair using jq
body: <p>I would updating a few keys in a kubernetes yaml file using yq / jq. The files contains multiple yaml documents for kubernetes deployments and services. I have managed to update the values of some keys. But I am stuck on updating the key value pairs in an array (env). I have it working when I hard code the array index. But this makes it brittle. Is there a correct way to look up the environment name and update it's value with a prefix.</p>
  
  <pre><code>apiVersion: extensions/v1beta1
  kind: Deployment
  metadata:
    name: zxl-portalsvc-deployment-$appEnv
  spec:
    replicas: 1 
    template: 
      metadata:
        labels:
          app: zxl-portalsvc-$appEnv
      spec:
        containers:
        - name: zxl-portalsvc
          image:  artifactory.oss.oxcart.com:5000/frontface-zxl-mca:5.6.4_17
          imagePullPolicy: Always
          resources:
            limits:
              cpu: "2"
              memory: 4096Mi
            requests:
              cpu: "1"
              memory: 2048Mi
          env:
          - name: MYROLE
            value: "zxl-portalsvc-service-$appEnv.frontface.svc.cluster.local"
          - name: MYAPPS
            value: "contentservices.war portalserver.war c1-integration.war"
          - name: tomcat_Xmx
            value: "3276m"
          ports:
          - containerPort: 8080
          volumeMounts:
            - name: frontface-configs
              mountPath: /usr/local/tomcat/frontface/config
            - name: sso-configs
              mountPath: /usr/local/tomcat/frontface/sso-config/idp_metadata.xml
              subPath: idp_metadata.xml
            - name: sso-configs
              mountPath: /usr/local/tomcat/frontface/sso-config/zxlKeystore.jks
              subPath: zxlKeystore.jks
            - name: tomcat-configs
              mountPath: /usr/local/tomcat/conf/context.xml
              subPath: context.xml
            - name: tomcat-configs
              mountPath: /usr/local/tomcat/conf/logging.properties
              subPath: logging.properties
            - name: tomcat-configs
              mountPath: /usr/local/tomcat/conf/server.xml
              subPath: server.xml
            - name: tomcat-configs
              mountPath: /usr/local/tomcat/conf/tomcat-users.xml
              subPath: tomcat-users.xml
            - name: tomcat-configs
              mountPath: /usr/local/tomcat/conf/web.xml
              subPath: web.xml
            - name: tomcat-configs
              mountPath: /usr/local/tomcat/bin/setenv.sh
              subPath: setenv.sh
  
        - name: zxl-nginxsvc
          image: artifactory.oss.oxcart.com:5000/oxcart-httpauth:1.3.0
          resources:
            limits:
              cpu: 250m
              memory: 200Mi
            requests:
              cpu: 200m
              memory: 100Mi
          env:
          - name: proxyDst
            value: "localhost"
          ports:
          - containerPort: 80
          volumeMounts:
            - name: nginx-configs
              mountPath: /etc/nginx/conf.d/nginx.htpasswd
              subPath: nginx.htpasswd
            - name: nginx-configs
              mountPath: /etc/nginx/conf.d/default.conf
              subPath: default.conf
  
        volumes:
        - name: frontface-configs
          configMap:
            name: frontface-config-$appEnv
        - name: sso-configs
          secret:
            secretName: sso-config-$appEnv
        - name: tomcat-configs
          configMap:
            name: tomcat-config-$appEnv
        - name: nginx-configs
          configMap:
            name: nginx-config-$appEnv
  
  ---
  
  apiVersion: v1
  kind: Service
  metadata:
    labels:
      name: zxl-portalsvc-service-$appEnv
    name: zxl-portalsvc-service-$appEnv
  spec:
    ports:
      - protocol: TCP
        port: 8080
    selector:
      app: zxl-portalsvc-$appEnv
  </code></pre>
  
  <p>I have it partially working except it is brittle. If the match is not the first element then it will break. Is there a way to fix resolve this. Maybe by identifying the array index of the match.</p>
  
  <p>Here is where I am at so far:</p>
  
  <pre><code>cat yq_test.yml | yq -y 'if .kind == "Deployment" 
      then (.metadata.name,.spec.template.metadata.labels.app) |= "ORG-" + "DEV-" +  sub("-\\$appEnv";"") | 
          if .spec.template.spec.containers[].env[].name == "MYROLE" then 
              .spec.template.spec.containers[].env[0].value  = "ORG-" + "DEV-" + .spec.template.spec.containers[].env[0].value 
          else empty end 
  elif .kind == "Service" then 
      (.metadata.name,.metadata.labels.name,.spec.selector.app) |= "ORG-" + "DEV-" +  sub("-\\$appEnv";"") 
  else . end'
  </code></pre>