When using basic string type configuration settings, the technique works effectively. What happens, though, if the configuration value is of a non-string type, such as a “map” or “list”?
Have a look at the values file below:
baseUrl: http://example.com
apiUrls:
apiOneUrl: “{{ .Values.baseUrl }}/apiOne/v0”
apiTwoUrl: “{{ .Values.baseUrl }}/apiTwo/v3”
apiThreeUrl: “{{ .Values.baseUrl }}/apiThree/v7”
In your template, you may attempt the following:
MyApiUrls: {{ tpl .Values.apiUrls . }}
The following template error will appear:
wrong type for value; expected string; got map[string]interface {}
The error message makes sense because the tpl function anticipates a template string as the first parameter. So how might we address the problem and make our solution applicable to this typical use case?
One option is to feed the non-string type value (in this example, map) to the toYaml function first and then convert it to a “YAML” string:
MyApiUrls:
{{ tpl (.Values.apiUrls | toYaml) . | indent 2 }}
If you use the aforementioned template, it will be appropriately displayed as:
MyApiUrls:
apiOneUrl: http://example.com/apiOne/v0
apiTwoUrl: http://example.com/apiTwo/v3
apiThreeUrl: http://example.com/apiThree/v7