Inject env var for all ConfigMap values Inject env var for all ConfigMap values kubernetes kubernetes

Inject env var for all ConfigMap values


You would have to use some kind of templating tool to process the YAML. Helm is popular (though that doesn't directly handle environment variables). For your case I would strongly recommend envsubst, it will handle this case nicely.


Like @coderanger mentioned you can use envsubst and Helm. I'll just provide tad more details and a another way ;)

envsubst

In your config_map.yml you would have:

severity= $LOG_SEVERITY

Then just create new environmental variable and execute kubectl in a following way:

export LOG_SEVERITY="9"envsubst < config_map.yml | kubectl apply -f -

Helm

Create a template and calling it.

kind: ConfigMapapiVersion: v1data:  log.properties: |    timeout=10    severity= {{ .Values.logSeverity }}

You would need to define a default value of severity inside mychart/values.yaml

logSeverity: 1

And setting a different value by adding --set flag to the helm call.

helm install --set logSeverity=9 ./mychart

This is nicely explained inside Helm documentation The Chart Template Developer’s Guide.Keep in mind this is way more complicated to just use helm as a wrapper for Kubernetes files.

sed

sed -i 's/$LOG_SEVERITY/9/g' input.txt

I recommend reading How to use sed to find and replace text in files in Linux / Unix shell.