consul-template

How do I use variable within consul-template range construct?


We have multiple environments for our apps, and I am trying to use generic consul template and an environment variable to create haproxy configurations for different environments. This is what I am trying to do:

{{$environment := "yellow" }} .... balance leastconn {{range service "myservice-{{$env}}"}} ....

I cannot seem to find a way to use this variable within this range. Is there a way to get this working?

Thanks!


Solution

  • You just need to define the variable and then use it in the range statement.

    {{ $service_name := env "CONSUL_SERVICE_NAME" }}
    
    {{range service $service_name }}