Helm Variables: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 13: Line 13:
</syntaxhighlight>
</syntaxhighlight>
Variables are less frequently used, as an alternative preferred construct is the [[Helm_Named_Templates|named template]].
Variables are less frequently used, as an alternative preferred construct is the [[Helm_Named_Templates|named template]].
Variables are particularly useful in range loops. They can be used on list-like objects to capture both the index and the value:
<syntaxhighlight lang='yaml'>
toppings: |-
  {{- range $index, $topping := .Values.pizzaToppings }}
    {{ $index }}: {{ $topping }}
  {{- end }}
</syntaxhighlight>
This will assign the integer index (0-based) to $index and the value to $topping.
For data structures that have both key and value, range can be used to get both and assign them to variables:
<syntaxhighlight lang='yaml'>
{{- range $key, $val := .Values.favorite }}
{{ $key }}: {{ $val | quote }}
{{- end }}
</syntaxhighlight>

Revision as of 00:59, 8 October 2020

External

Internal

Overview

A variable is a named reference to another object. It is assigned and referred using the $<name> syntax. := is used as assignment operator.

{{- $relname := .Release.Name -}}
...
data:
  release: {{ $relname }}

Variables are less frequently used, as an alternative preferred construct is the named template.

Variables are particularly useful in range loops. They can be used on list-like objects to capture both the index and the value:

toppings: |-
  {{- range $index, $topping := .Values.pizzaToppings }}
    {{ $index }}: {{ $topping }}
  {{- end }}

This will assign the integer index (0-based) to $index and the value to $topping. For data structures that have both key and value, range can be used to get both and assign them to variables:

{{- range $key, $val := .Values.favorite }}
{{ $key }}: {{ $val | quote }}
{{- end }}