Consul-template: index NodeMeta data fail

Created on 1 Mar 2017  路  5Comments  路  Source: hashicorp/consul-template

HI,

I want to access the meta data for a service. As the doc says, To access map data such as NodeTaggedAddresses or NodeMeta, use Go's text/template map indexing., but when I put {{index .NodeMeta "port"}} in haproxy.tmpl, I got the following error

But it seems NodeMeta is returned in healthservice struct
https://github.com/hashicorp/consul-template/blob/master/dependency/health_service.go#L45

The consul-template version is v0.18.1, and I am running CT as its own(not by nomad)

Consul Template version

consul-template v0.18.1 (9c62737)

Configuration

{{ range services }}
frontend {{ .Name }}_frontend
    mode tcp
    bind *:{{index .NodeMeta "port"}}
    use_backend {{ .Name }}_backend
{{ end }}

Command

consul-template -config=/opt/consul-template/consul-template.hcl

Expected behavior

What should have happened?

NodeMeta should be parsed in template

Actual behavior

Error happened.

/opt/consul-template/haproxy.ctmpl: execute: template: :29:20: executing "" at <.NodeMeta>: can't evaluate field NodeMeta in type *dependency.CatalogSnippet

Most helpful comment

In case anyone else is banging their head against the wall JUST trying to access metadata from the current node, you can do it as follows:

{{with node}}
        {{index .Node.Meta "key" }}
{{end}}

where key is the...meta key you are trying to access the value for.

All 5 comments

Hi @waterdudu

When you open a new issue, there's a nice template that pre-fills the textbox and prompts for various pieces of information, including your configuration and the debug output. Could you please supply that information? Thanks!

@sethvargo updated

Hi @waterdudu

I think you are conflating service and services. Services queries the _catalog_ services endpoint, which returns JSON like this:

{
  "service1: ["tag1", "tag2"],
  "service2: ["tag2", "tag3"]
}

There is no NodeMeta attribute on the CatalogSnippet. You will need to query for a health service to get that:

{{ range services }}
{{ range service .Name }}
frontend {{ .Name }}_frontend
    mode tcp
    bind *:{{index .NodeMeta "port"}}
    use_backend {{ .Name }}_backend
{{ end }}
{{ end }}

Please add an NodeMeta example in the https://github.com/hashicorp/consul-template.

Below works for me but was frustrating to get to this page and finally find it

{{ range services }}
{{ range service .Name }}
frontend {{ .Name }}_frontend
mode tcp
bind *:{{index .NodeMeta "port"}}
use_backend {{ .Name }}_backend
{{ end }}
{{ end }}

Thanks

In case anyone else is banging their head against the wall JUST trying to access metadata from the current node, you can do it as follows:

{{with node}}
        {{index .Node.Meta "key" }}
{{end}}

where key is the...meta key you are trying to access the value for.

Was this page helpful?
0 / 5 - 0 ratings