Consul-template: Generating a comma-separated list of node names from a service

Created on 29 Jul 2015  路  3Comments  路  Source: hashicorp/consul-template

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?

Most helpful comment

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.

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Dentrax picture Dentrax  路  5Comments

bryanlarsen picture bryanlarsen  路  7Comments

carlpett picture carlpett  路  5Comments

scalp42 picture scalp42  路  6Comments

aferreira picture aferreira  路  3Comments