<p>I have around 200-300 yaml files lying around and What I am trying to achieve is to change only the image: option in few of my yaml files, I have a shell script which can get the required yaml files, and I just want to achieve changing the image: key only.</p>
<p><strong>Solution Tried:</strong></p>
<p>Used a tool <a href="https://mikefarah.gitbook.io/yq/" rel="nofollow noreferrer">yq</a> v4:</p>
<pre><code>for i in ${deployment[@]}
 do
 yq eval '( select(.spec.template.spec.containers[0].image |= &quot;gcr.io/myrepo/mynginx:1.2.3&quot;'  deployment-$i.yaml
done
</code></pre>
<p>What this command does is it achieves the desired result of changing the image, but it also appends the changes on the all the parts of deployment file which is for example at the end of every spec in the file:</p>
<pre><code>        apiVersion: apps/v1
        kind: Deployment
        metadata:
          name: portal-1
        spec:
          selector:
            matchLabels:
              component: portal-1
              role: ui
          replicas: 1
          template:
            metadata:
              labels:
                component: reactportal
                role: ui
            spec:
              containers:
                - name: portal
                  image: gcr.io/myrepo/mynginx:4.52 &lt;&lt;&lt; Desired CHange Happens Here &gt;&gt;&gt;
        ---
        
        apiVersion: extensions/v1beta1
        kind: Ingress
        metadata:
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /$1
        nginx.ingress.kubernetes.io/enable-cors: &quot;false&quot;
        nginx.ingress.kubernetes.io/cors-allow-origin: &quot;https://example.com&quot;
      name: portal-ingress
    spec:
      rules:
        - host: mydomain.com
          http:
            paths:
              - backend:
                  serviceName: portal-svc
                  servicePort: 80
                path: /(.*)
 template:                                                  &lt;&lt;&lt;&lt; THIS IS ALSO GETTING APPENDED &gt;&gt;&gt;
        spec:
          containers:
            - image: gcr.io/myrepo/mynginx:1.2.3
</code></pre>
<p>How to achieve the desired result without appending the image on every part of the deployment file?</p>

Chronograph3r
<p>This should get you the expected result :</p>
<pre><code>for i in ${deployment[@]}; do
  yq eval '.spec|=select(.selector.matchLabels.component==&quot;portal-1&quot;)
                 .template.spec.containers[0].image =
                    &quot;gcr.io/myrepo/mynginx:1.2.3&quot;' deployment-$i.yaml
done
</code></pre>
