<p>I have this command in a bash script</p>
<pre><code>kubectl get svc --selector='app.kubernetes.io/component=sentinel' --all-namespaces -o json |
jq -r '
.items
   | map(
   {label:.metadata.name,
   sentinels: [{host:(.metadata.name + &quot;.&quot; + .metadata.namespace + &quot;.&quot; + &quot;svc&quot; + &quot;.&quot; + &quot;cluster&quot; + &quot;.&quot; + &quot;local&quot;),port: .spec.ports[0].port}],
   sentinelName:&quot;mymaster&quot;,
   sentinelPassword: &quot;&quot;
   dbIndex: 0
   })
| {connections: . }' /
&gt;local.json
</code></pre>
<p>which produces something like this output</p>
<pre class="lang-json prettyprint-override"><code>{
  &quot;connections&quot;: [
    {
      &quot;label&quot;: &quot;Redis1&quot;,
      &quot;sentinels&quot;: [
        {
          &quot;host&quot;: &quot;Redis1.default.svc.cluster.local&quot;,
          &quot;port&quot;: 26379
        }
      ],
      &quot;sentinelName&quot;: &quot;mymaster&quot;,
      &quot;sentinelPassword&quot;: &quot;&quot;,
      &quot;dbIndex&quot;: 0
    },
    {
      &quot;label&quot;: &quot;Redis2&quot;,
      &quot;sentinels&quot;: [
        {
          &quot;host&quot;: &quot;Redis2.development.svc.cluster.local&quot;,
          &quot;port&quot;: 26379
        }
      ],
      &quot;sentinelName&quot;: &quot;mymaster&quot;,
      &quot;sentinelPassword&quot;: &quot;&quot;,
      &quot;dbIndex&quot;: 0
    }
  ]
}
</code></pre>
<p>This config file is injected into container via an init-container so Redis-Commander fetches the redis instances without the user having to manually input any connection config data. This works fine however but one of the instances requires a <code>sentinelPassword</code> value.</p>
<p>I can fetch the password using <code>kubectl get secret</code> but I'm trying to figure out how to insert that password into the config file for the particular instance that requires it.</p>
<p>I've been trying something along the lines of this but getting my jq syntax wrong. Any help or alternative ways of going around would be appreciated.</p>
<pre class="lang-sh prettyprint-override"><code>#store output in var
JSON=$(kubectl get svc --selector='app.kubernetes.io/component=sentinel' --all-namespaces -o json |
jq -r '
.items
   | map(
   {label:.metadata.name,
   sentinels: [{host:(.metadata.name + &quot;.&quot; + .metadata.namespace + &quot;.&quot; + &quot;svc&quot; + &quot;.&quot; + &quot;cluster&quot; + &quot;.&quot; + &quot;local&quot;),port: .spec.ports[0].port}],
   sentinelName:&quot;mymaster&quot;,
   sentinelPassword: &quot;&quot;,
   dbIndex: 0
   })
| {connections: . }')

# Find instance by their host value which is unique. (Can't figure out how to do this bit)
  if $JSON host name contains &quot;Redis2.development.svc.cluster.local&quot;
#then do something like this
    &quot;$JSON&quot; | jq '.[]'| .sentinelPassword = &quot;$password&quot; #var stored from kubectl get secret cmd

#save output to file
&quot;$JSON&quot;&gt;/local.json

</code></pre>

<p>Assuming for the moment that you want to invoke jq on the sample JSON (local.json) as shown, you could run:</p>
<pre><code>password=mypassword
&lt; local.json jq --arg password &quot;$password&quot; '
  .connections[] |= if any(.sentinels[].host; index(&quot;Redis2.development.svc.cluster.local&quot;))
                    then .sentinelPassword = $password else . end'
</code></pre>
<p>However, if possible, it would probably be better to invoke jq just once.</p>
