Home > Net >  Create configMap with the key as variable(Kubernetes)
Create configMap with the key as variable(Kubernetes)

Time:01-26

I want to create config map from file with key as value of variable from kubectl tool

However,

MY_VARIBLE="something"
kubectl create configmap myconfigmap --from-file=${MY_VARIBLE}=myfile.json

does not return anything and configmap is not created

kubectl create configmap myconfigmap --from-file=something=myfile.json

works correctly. How can I workaround it? Thanks!!

CodePudding user response:

It looks like the problem is that the variable MY_VARIBLE is not being expanded correctly when passed to the kubectl command. One way to fix this is to use double quotes ("") when defining the variable.

MY_VARIBLE="something"
kubectl create configmap myconfigmap --from-file="${MY_VARIBLE}=myfile.json"

This will allow the variable to be expanded correctly and passed to the kubectl command.

Alternatively, you can use command substitution to pass the variable's value to the kubectl command like this:

MY_VARIBLE="something"
kubectl create configmap myconfigmap --from-file=$MY_VARIBLE=myfile.json

This will substitute the value of the variable into the command and the command will work as expected.

CodePudding user response:

Thank you all! My real problem was that MY_VARIABLE had value with ":" After replacing ":" with "-" everything started working correctly in any case from above

  • Related