Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use kubectl command to get a value from a yaml file inside a k8s configmap?

Assume that the configmap is listed as follows:

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

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

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

like image 320
injoy Avatar asked Oct 20 '25 09:10

injoy


1 Answers

You can get the data.my-config.yaml value either by using jsonpath or go-template.

Example with jsonpath:

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

Example with go-template:

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

Note that by using |- on your YAML, you are defining a Multiline YAML String, which means that the returned value is a single string with line breaks on it (\n).

If you want only the keyB value, you can use your output to feed a YAML processor like yq. E.g:

$ kubectl get cm my-configmap -o 'go-template={{index .data "my-config.yaml"}}' | yq -r .keyA.keyB
a-value
like image 67
Eduardo Baitello Avatar answered Oct 22 '25 01:10

Eduardo Baitello



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!