Following is my code to render the Addresses of the nodes with a service:
{{range "service@datacenter" "passing"}}{{.Address}} {{end}}
What I want to do is limit the number of addresses that are rendered. For example, if there are 5 nodes registered as providers of "service", I would like is to print addresses of 2 of them only. What I think I should be doing is slicing the array, but I am not able to get the GO syntax right. This is something that I want but is not syntactically correct:
{{range "service@datacenter" "passing" [0:2]}}{{.Address}} {{end}}
What is the correct way to do this?
Found a solution to limit the number of array elements that can be rendered using consul-template.
{{$x := service "service@datacenter" "passing"}}{{range $index, $element := $x}}{{if lt $index 2}}{{$element.Address}},{{end}}{{end}}
The range function provides an index variable along with the array element that can be used to check for the limit using the if block. Here the only two Addresses would be rendered.