Assign variable inside "if" condition in Helm templating

6/10/2019

I'm using Helm templating and trying to assign an variable inside an "IF " condition as follows. The variable is "dbpassword". `

  {{- if eq .Values.dbType "mysql" }}
    {{- $dbpassword := "dd" }}
  {{ end}}
  {{- $file := .Files }}
  {{- range $path, $byte := .Files.Glob "confs/axis2/*" }}
  {{- $list := $path | splitList "/"}}
  {{- $length := len $list }}
  {{- $last := add $length -1 }}
  {{ index $list $last }}: |-
    {{- range $line := $file.Lines $path }}
      {{ $line | replace "DATABASE_USERNAME" $dbpassword }}
    {{- end }}
  {{- end }}
`

While trying to use the defined variable to replace the string "DATABASE_USERNAME", it throws an error saying "undefined variable dbpassword". When the variable is defined outside the "if" condition, it works properly.

Any help is appreciated.

-- TSC1192
if-statement
kubernetes-helm
templating
variable-assignment

0 Answers