I have a number of consul nodes that look something similar to:
[
{
"Address": "127.0.0.1",
"Node": "foo",
"ServiceAddress": "",
"ServiceName": "api",
"ServicePort": 8100,
"ServiceTags": [
"production",
"blocking"
]
},
{
"Address": "127.0.0.1",
"Node": "foo",
"ServiceAddress": "",
"ServiceName": "api",
"ServicePort": 8101,
"ServiceTags": [
"production",
"nonblocking"
]
}
]
Filtering by one tag is easy:
{{range service "production.api"}}
{{.Address}}
{{end}}
but how can I filter services within my consul-template by two tags at once?
As of consul-template v0.11.1 you can use the contains
operator to do:
{{range service "production.api"}}
{{if .Tags | contains "nonblocking"}}
{{.Address}}
{{end}}
{{end}}
If you are using an older version you can take advantage of Go:
{{range service "api"}}
{{if and (.Tags.Contains "nonblocking") (.Tags.Contains "production")}}
{{end}}
{{end}}
see also: https://github.com/hashicorp/consul-template/issues/260