confd icon indicating copy to clipboard operation
confd copied to clipboard

Confd doesn't work with yaml list

Open jinsiang2008 opened this issue 5 years ago • 4 comments

I have a yaml file format like:

sshd:
  pubkeys:
    user1:
      - key1
      - key2
    user2:
      - key1

Then I would like to use confd template to generate a config file based on it. Looks like confd doesn't like the formate, I tried the following template:

{{range gets "/sshd/pubkeys/*"}}
{{.}}
{{end}}

But it returned nothing. Could someone take a look?

Thank you

jinsiang2008 avatar Jan 02 '20 20:01 jinsiang2008

Also I tried:

{{range $pub_key := ls "/sshd/pubkeys/user1"}}
{{$pub_key}}
{{end}}

I expected it will return key1 and key2 to me, but instead, I got value 0 and 1, so the index. How could I get the value?

jinsiang2008 avatar Jan 03 '20 00:01 jinsiang2008

Yeah, I think it's a bug since ls to a yaml list returned the index instead of actually value.

{{range $pub_key := ls "/sshd/pubkeys/user1"}}
{{$pub_key}}
{{end}}

Returned:

0
    
1

jinsiang2008 avatar Jan 03 '20 16:01 jinsiang2008

Hi there, I was having the same problem trying to print a string list, it saved the values comma separated and I tried with {{ range $user := split (getv "/vars/users") "," }} - {{ $user }} {{end}}

and it worked :) other approach I tried and worked with other kind of variables was {{ range gets "/users/*" }} - {{ base .Key }}: '{{ .Value }}' {{ end }}

I think your problem solutions is more or less related to those solutions but i dont know how is your variable being saved into ssm or another format :3

Lucywaa avatar Feb 19 '20 23:02 Lucywaa

I had a similar issue (tried to work with Kubernetes YAMLs) and, indeed, the lists do not seem to work as expected. I get empty objects.

I was able to work around the issue by doing, essentially, this:

{{ range ls "/items" }} {{ $index := (atoi . )}} {{ $ip_key := (print "/items/" $index "/status/podIP") }} {{ if exists $ip_key }} {{ getv $ip_key }} {{ end }} ... {{ end }}

Of course, I needed more than one field and some conditional processing too, so this just demonstrates the idea. Getting the list of available indexes and then building the path expressions using the index for building the full path names of the values I am interested in.

By the way, turns out that ./resource/template/template_funcs.go contains some good stuff that does not seem to be included in the docs ;)

ngrigoriev avatar Jun 23 '20 20:06 ngrigoriev