Not able to refer the index inside the range function in yaml files

759 views Asked by At

I am trying dynamically assigning the values from separate values.yaml file to a variable.

My values.yaml file content

peer_cidr1 = x
peer_cidr2 = y
peer_cidr3 = z

Yaml file:

   {{- $root := . -}}
   {{ range $i, $dn := until (atoi (printf "%d" (int64 .Values.no_of_peers))) }}
   { "dst": "{{ $root.Values.peer_cidr$i }}" }

Referring as Values.peer_cidr$i to refer variables from values.yaml. $i is throwing bad character error.

1

There are 1 answers

1
icza On BEST ANSWER

$root.Values.peer_cidr$i is invalid syntax in templates.

Instead use the index function, e.g.

{{ index $root "Values" (printf "peer_cidr%d" $i) }}

Also note that the index stats at 0 but the values you want to look up start at 1 (e.g. peer_cidr1), so the value for index 0 will not exist, and you will not visit the last element. So add 1 to the $i like this:

{{ index $root "Values" (printf "peer_cidr%d" (add1 $i) ) }}