I'm trying to generate a config file for Icinga, and at one point I need a comma-separated list of nodes, without a trailing comma.
It feels as though something like this should work;
{{define "NODES"}}{{range service "loadbalancer" "any"}} {{.Node}}{{end}}{{end}}
{{template "NODES" | split " " | join "," }}
But, that gives the error: unexpected "|"
Is there a way to do this without writing a plugin?
Hi @digitalronin
There is no concept of a map function in go's template language (which is what you're trying to do here). Users have found success with the following in other tickets: https://github.com/hashicorp/consul-template/issues/263#issuecomment-97074185.
I'm going to close this issue since there really isn't a good solution (but there are good workarounds), but I'm more than willing to continue discussion.
Thanks so much, that's just what I needed. This works;
{{range $index, $service := service "loadbalancer" "any"}}{{if ne $index 0}},{{end}}{{.Node}}{{end}}
It's pretty fugly though.
https://groups.google.com/forum/#!topic/consul-tool/j8O9UPOqfMA
Looks like they allow conditional comma placement. Would this be a good solution? Separating the data flow and control flow is always preferred. So python will have service lookup and json load, while json file dumps everything from consul.
Most helpful comment
Thanks so much, that's just what I needed. This works;
It's pretty fugly though.