go - How to concatenate Service metadata for consul-template with commas -
does know how concatenate strings consul consul-template?
if have service 'foo' registered in consul
{ "node": "node1", "address": "192.168.0.1", "port": 3333 }, { "node": "node2", "address": "192.168.0.2", "port": 4444 }
i consul-template generate following line:
servers=192.168.0.1:3333,192.168.0.2:4444/bogus
the following attempt not work since leaves trailing comma ,
servers={{range service "foo"}}{{.address}}{{.port}},{{end}}/bogus # renders servers=192.168.0.1:3333,192.168.0.2:4444,/bogus # want servers=192.168.0.1:3333,192.168.0.2:4444/bogus
i know consul-template uses golang template syntax, cannot figure out syntax working. should use consul-template's join
how pass both .address
, .port
join
? trivial example, , i'm not using indexes intentionally since number of services more two. ideas?
this should work.
{{$foo_srv := service "foo"}} {{if $foo_srv}} {{$last := len $foo_srv | subtract 1}} servers= {{- range $i := loop $last}} {{- index $foo_srv $i}}{{.address}}{{.port}},{{end}} {{- end}} {{- index $foo_srv last}}{{.address}}{{.port}}{{end}}/bogus {{end}}
i thinking if "join" can used.
note "{{-" means removing leading white spaces (such ' ', \t, \n).
Comments
Post a Comment