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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With