<p>Assume that the configmap is listed as follows:</p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
  namespace: ${namespace}
data:
  my-config.yaml: |-
    keyA:
      keyB: a-value
</code></pre>

<p>How can I get the value of <code>keyB</code> (which is <code>a-value</code>) from the configmap using the <code>kubectl</code> command? </p>

<p>PS: I was thinking of using <code>-o jsonpath</code> or <code>-o 'go-template=...</code> options, but I couldn't figure out the correct syntax.</p>

injoy
<p>You can get the <code>data.my-config.yaml</code> value either by using <code>jsonpath</code> or <code>go-template</code>.</p>

<p>Example with <code>jsonpath</code>:</p>

<pre><code>$ kubectl get cm my-configmap -o "jsonpath={.data['my-config\.yaml']}"
keyA:
  keyB: a-value
</code></pre>

<p>Example with <code>go-template</code>:</p>

<pre><code>$ kubectl get cm my-configmap -o 'go-template={{index .data "my-config.yaml"}}'
keyA:
  keyB: a-value
</code></pre>

<p>Note that by using <code>|-</code> on your YAML, you are defining a <a href="https://yaml-multiline.info/" rel="noreferrer">Multiline YAML String</a>, which means that the returned value is a single string with line breaks on it (<code>\n</code>).</p>

<p>If you want only the <code>keyB</code> value, you can use your output to feed a YAML processor like <a href="https://github.com/kislyuk/yq" rel="noreferrer"><code>yq</code></a>. E.g:</p>

<pre><code>$ kubectl get cm my-configmap -o 'go-template={{index .data "my-config.yaml"}}' | yq -r .keyA.keyB
a-value
</code></pre>
