consul-template and services current node

582 views
Skip to first unread message

Gavin M. Roy

unread,
Mar 6, 2015, 5:03:23 PM3/6/15
to consu...@googlegroups.com
Is there a way to get the node consul-template is being run on in a consul template? I'm specifically looking to get the tags for a service running on the node that the template is being rendered on.

Absent the ability to get this, I've thought about setting an environment variable with this value. Are there any other ideas on how I can accomplish this?

Regards,

Gavin

Seth Vargo

unread,
Mar 6, 2015, 5:08:08 PM3/6/15
to Gavin M. Roy, consu...@googlegroups.com
Hi Gavin,

When Consul Template communicates with Consul, it does so via the HTTP API. There is no requirement that the node you are running on even has consul installed. This means Consul Template is unaware of the "current node" in Consul. The good news, however, is that Consul Template comes with a built-in `env` function that can read an environment variable you set.

{{env "MY_ENV"}}

Does that help?

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.

Gavin M. Roy

unread,
Mar 6, 2015, 5:26:43 PM3/6/15
to Seth Vargo, consu...@googlegroups.com
Thanks for the info, that was my fallback plan. Perhaps you have some insight in how I could achieve the following since I'm just picking up the Go templating language:

I have a service named foo with n nodes, each node has multiple tags. I want to iterate on the tags that match the current node. I'm planning on having a HOSTNAME=`hostname -f` env var, so I am running:

    HOSTNAME=`hostname -f` consul-template -config=/etc/consul-template.d/ -dry -once 

I'm creating a YAML config file and hope this snippet explains what I'm trying to get to:

      {{range $tag, $services := service "foo" || node := env($HOSTNAME) | byTag}}
      {{.Tag}}:
          consumer: {{key "service/foo/consumer/{{.Tag}}/class"}}
      {{end}} 

So for each tag in the service foo iterate the tags for this node, and then get key/value pairs based upon the tag being part of the KV tree.

Is this possible? If so, how would you go about it?

Gavin M. Roy

unread,
Mar 6, 2015, 5:54:34 PM3/6/15
to Seth Vargo, consu...@googlegroups.com
It's ugly, but this is what I came up with:

      {{with $hostname := env "HOSTNAME"}}{{range service "rejected"}}{{if eq .Node $hostname}}{{range .Tags}}
      {{.}}:
          consumer: {{with $k := printf "service/rejected/consumers/%s/consumer" (.) }}{{ key $k }}{{end}}
      {{end}}{{end}}{{end}}{{end}}

Gavin M. Roy

unread,
Mar 6, 2015, 6:03:43 PM3/6/15
to Seth Vargo, consu...@googlegroups.com
So the last thing I'm struggling with here is I have a folder based key that can look like:

    foo = 1
    bar = 2
    baz/qux = 3
    baz/corgie = 4

And I want to output

    foo: 1
    bar: 2
    baz:
      qux: 3
      corgie: 4

but when I'm using tree, I'm getting:

    foo = 1
    bar = 2
    baz/qux = 3
    baz/corgie = 4

Any suggestions on how to handle that? Thanks!

Gavin M. Roy

unread,
Mar 6, 2015, 6:09:40 PM3/6/15
to Seth Vargo, consu...@googlegroups.com
And I think I see how to do that with |byKey but it's not released yet, is there a ETA for 0.8.0 with this new feature?

Cheers,

Gavin
Reply all
Reply to author
Forward
0 new messages