Consul-template - Service tag interpolation in key lookup

777 views
Skip to first unread message

Nick Wales

unread,
Feb 6, 2015, 12:12:40 AM2/6/15
to consu...@googlegroups.com
I'm trying to iterate over a service with multiple tagged versions, grabbing a load balanced weight value from the key value store based on the tag and  putting that in the key request path but having no luck. 

The key/value path is "test_service/1.x/lb_weight" and so far the snippet looks like this, it returns what looks like a working template but I get the string rather than the K/V response in my haproxy config file.



 {{range $test_service := service "test_service"}}
     
{{range $tags := .Tags}}
     
{{$weight := print "{{key \"test_service/" $tags "/lb_weight\"}}"}}
        server
{{$test_service.Name}} {{$test_service.Address}}:{{$test_service.Port}} weight {{$weight}}
     
{{end}}
 
{{end}}

Response:

    server test_service 172.28.11.165:45000 weight {{key "test_service/1.0/lb_weight"}}
    server test_service 172.28.11.165:45001 weight {{key "test_service/1.1/lb_weight"}}

I'm running consul-template 0.6.0, any help much appreciated!

Seth Vargo

unread,
Feb 6, 2015, 8:43:23 AM2/6/15
to Nick Wales, consu...@googlegroups.com
Hi Nick,

Go's tempplating language does not permit nested evaluation like that. I think you want something like this instead:

 {{range $test_service := service "test_service"}}
  {{range $tags := .Tags}}
    {{$weight := key ($tag | printf "test_service/%s/lb_weight")}}
    server {{$test_service.Name}} {{$test_service.Address}}:{{$test_service.Port}} weight {{$weight}}
  {{end}}
 {{end}}

You still need to make a raw call to `key` in the top-level evaluation.

Best,
Seth

--
You received this message because you are subscribed to the Google Groups "Consul" group.
To unsubscribe from this group and stop receiving emails from it, send an email to consul-tool...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nick Wales

unread,
Feb 6, 2015, 10:19:59 AM2/6/15
to consu...@googlegroups.com, ni...@nickwales.co.uk
Excellent, that totally works.

Thanks for your help!
Reply all
Reply to author
Forward
0 new messages