range — Loops
range iterates over a list or map to render repeated blocks, such as multiple env vars or hosts.
{{- range .Values.list }}
- {{ . }}
{{- end }}range loops over a list or map. It is how you turn a values array into repeated YAML.
Over a list
Inside the block, the dot becomes each element in turn.
{{ range .Values.hosts }} - {{ . }} {{ end }}Over a map / with index
Capture the key and value: {{ range $key, $val := .Values.labels }}. For lists you can capture the index too: {{ range $i, $v := .Values.items }}.
Reaching outside the loop
Like with, range rebinds the dot. Use $ to get back to the root context inside the loop.
Example
env:
{{- range .Values.extraEnv }}
- name: {{ .name }}
value: {{ .value | quote }}
{{- end }}
tls:
{{- range $host := .Values.ingress.hosts }}
- host: {{ $host }}
secretName: {{ $.Release.Name }}-tls
{{- end }}When to use it
- A chart template uses range over .Values.env to render one env var entry per item, letting operators define an arbitrary list of environment variables in values.yaml.
- A developer iterates over .Values.ingress.hosts with range to generate one Ingress rule per host without duplicating the rule block for each hostname.
- A chart iterates over a map of ConfigMap data entries using range to produce key-value pairs, making the ConfigMap fully driven by values.yaml.
More examples
Range over a list
Iterates over an array of env-var objects in values.yaml and renders one env entry per item in the container spec.
# values.yaml: env: [{name: LOG_LEVEL, value: info}, {name: PORT, value: "8080"}]
env:
{{- range .Values.env }}
- name: {{ .name | quote }}
value: {{ .value | quote }}
{{- end }}Range over a map
Iterates a key-value map from values.yaml, assigning each pair to $key and $val variables to build ConfigMap data.
# values.yaml: config: {DEBUG: "false", MAX_CONN: "100"}
data:
{{- range $key, $val := .Values.config }}
{{ $key }}: {{ $val | quote }}
{{- end }}Range with index
Captures the loop index $i and the element $host, generating one Ingress rule per host entry in the values list.
rules:
{{- range $i, $host := .Values.ingress.hosts }}
- host: {{ $host.name | quote }}
http:
paths:
- path: {{ $host.path | default "/" }}
pathType: Prefix
{{- end }}
Discussion