consul-template: query by service.tag

2,078 views
Skip to first unread message

Ivan Savčić

unread,
Aug 12, 2016, 12:10:15 PM8/12/16
to Consul
Hello,

I have troubles iterating over service.tag.

This doesn't work:

{{ range service "helloworld.prod" }}
  {{ .Address }}:{{ .Port }} {{ .Tags }}
{{ end }}

This does work:

{{ range service "helloworld" }}
  {{ if .Tags | contains "prod" }}
    {{ .Address }}:{{ .Port }} {{ .Tags }}
  {{ end }}
{{ end }}

helloworld has instances with only [prod] or with [prod foo] tags, the output of second template looks like:

x.x.x.x:a [foo prod]
y.y.y.y.b [prod]

Any ideas on what I am doing wrong?

Consul server is v0.6.3, consul-template is v0.15.0

Thanks!

Ivan

Ivan Savčić

unread,
Aug 12, 2016, 12:34:27 PM8/12/16
to Consul
Just to add, by "doesn't work" I mean "it returns nothing", there is no error.

Ivan

Kevin Lee

unread,
Aug 13, 2016, 2:50:10 AM8/13/16
to Consul
Try putting the tag before the service name like you would typically do when looking up a service by tag in consul.

tag.service
prod.helloworld

https://github.com/hashicorp/consul-template/issues/33

Ivan Savčić

unread,
Aug 13, 2016, 6:33:40 AM8/13/16
to Consul
Brilliant, thank you! It works like a charm. I've been looking at the documentation and the issues of course, but I somehow permuted those two... Thanks again


Ivan

james....@made.com

unread,
Aug 14, 2017, 6:39:41 AM8/14/17
to Consul
FWIW This is how I am using service tags in haproxy

{{ range $tag, $services := service "some-service" | byTag }}
backend some-service-{{ $tag }}

    {{ if eq $tag "something" }}
    ....
    {{ end }}
    ...
    {{ range $services }}
    server {{.Address}}-{{.Port}} {{.Address}}:{{.Port}} check downinter 3s inter 2000 fall 3 maxconn 100 check cookie {{.ID}} weight 1
    {{ end }}
{{ end }}

James

Reply all
Reply to author
Forward
0 new messages