consulconsul-template

how to retrieve limited number of IPs when Consul service catalog is queried


Is there a way to retrieve only 3 IPs from Consul service catalog?

{{if service 'web' 'passing'}}
 "{{range $index, $service := service 'web'}}{{if ne $index 0}},{{end}}{{.Address}}{{end}}"

The above code gets all comma separated IPs (I have 100's of IPs) that passes the web service check . I just need first 3 IPs.

-W


Solution

  • {{range $index, $service := service "web"}}{{if gt $index 3}}{{.Address}}:{{ .Port }},{{end}}{{end}}

    should do it.