janeczku / go-rancher-gen

:cow: Generate configuration files using templates and Rancher Metadata
MIT License
32 stars 4 forks source link

Only get one result from iteration #4

Open HTPC-Schrauber opened 7 years ago

HTPC-Schrauber commented 7 years ago

Hi,

I need to loop over containers of a service and get the IP addresses of the containers in state running. So far this is simple. This would be the following template:

{{ with service }}
  {{ range .Containers }}
    {{ if eq .State "running" }}
IP: {{ .Address }}
    {{ end }}
  {{ end }}
{{ end }}

But now I need to get only one IP. Ideally the first. And if no container is running, the IP I get should be "127.0.0.1".

Is there a way to do this? Writing the IPs to a variable inside the iteration does not work, because I do not have access to the variable outside of the iteration.

Regards