Nested range with Consul-template

Hi all!

I can’t wrap my head around how to loop over elements returned by another loop with Consul-template.

My secrets are stored this way :

secret/data/service/host1/var1 → foo1/bar1
secret/data/service/host1/var2 → foo2/bar2
secret/data/service/host2/var1 → foo3/bar3
secret/data/service/host2/var2 → foo4/bar4

The Consul-template templating language page show an example to get all the elements with a simple loop like this :

{{ range secrets "secret/" }}
{{ with secret (printf "secret/%s" .) }}{{ range $k, $v := .Data }}
{{ $k }}: {{ $v }}
{{ end }}{{ end }}{{ end }}

In my case, I guess I have to use another “range” nested in the first one to go over all the entries.

My try is something that look like this :

{{ range secrets "secret/data/service" }}
{{ $entry_name := (printf "%s" .) }}
{{ range secrets (printf "secret/data/service/%s" .) }}
{{ with secret (printf "secret/data/service/$entry_name/%s" .) }}{{ range $k, $v := .Data.data }}
{{ $k }}: {{ $v }}
{{ end }}{{ end }}
{{ end }}
{{ end }}

I would like to get something like this :

foo1: bar1
foo2: bar2
foo3: bar3
foo4: bar4

How would you do that?

Thanks for your help!

Good day!

Just so you know, I just found out… :slight_smile:

{{ range secrets "secret/data/service" }}
{{ $host := . }}
{{ range secrets (printf "secret/data/service/%s" .) }}
{{ $var := . }}
{{ with secret (print "secret/data/service/" $host $var) }}{{ range $k, $v := .Data.data }}
{{ $k }}: {{ $v }}
{{ end }}{{ end }}
{{ end }}
{{ end }}