modify variable in outer scope of a Kubernetes Helm template

10/18/2018

I can append characters to a variable (i.e. concatenate) in Helm like that

{{- $myvar := "foo" }}
{{- $myvar := printf "%s-%s" $myvar "bar" }}
myvar: {{ $myvar }} # myvar: foo-bar

I do need this concatenation when iterating over files. Unfortunately - probably due to the variable scope - changing the variable is local to the scope. Let's use tuples for the sake of simplicity:

data:
{{- $myvar := "foo" }}
{{- $myvar := printf "%s-%s" $myvar "bar" }}
{{- range tuple "vala" "valb" "valc" }}
  {{- $myvar := printf "%s-%s" $myvar . }}
  {{ . }}: {{ $myvar }}
{{- end }}
  myvar: {{ $myvar }}

is rendered as

data:
  vala: foo-bar-vala
  valb: foo-bar-valb
  valc: foo-bar-valc
  myvar: foo-bar

How do I get the code rewritten, so I can actually append values to the $myvar variable in the outer scope?

-- muffel
kubernetes-helm

0 Answers