Getting a single service instance with consul-template

784 views
Skip to first unread message

Jeff Fouchard

unread,
Nov 10, 2015, 8:40:03 AM11/10/15
to Consul
Hello,

Doing some initial exploration with consul-template and have hit a bit of stumbling block.  Is there an easy way to return a single instance of a service without resorting to DNS? I may be dense but the examples all seem to show one line per instance. Thats fine for HAProxy, but sometimes we need just one.  Currently the environment is not guaranteed to have consul as DNS endpoint.  

Thanks,
Jeff

Armon Dadgar

unread,
Nov 10, 2015, 1:15:22 PM11/10/15
to consu...@googlegroups.com, Jeff Fouchard
Hey Jeff,

You basically just want to query the service which will return a list of IPs and just consume one of them.
For example:

{{ with $instances := service “my-service” }}
{{ if len $instances > 0 }}
    {{ with $first := index $instances 0 }}
    my-service: {{ first.Addr }}
{{ end }}

This has not been tested, but something like that should work.

Best Regards,
Armon Dadgar
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/consul/issues
IRC: #consul on Freenode
---
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/consul-tool/32886048-8a0d-4197-87f1-e3462bfd93bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages