Consul-template: retrieve value from a specific key

Created on 12 May 2017  路  2Comments  路  Source: hashicorp/consul-template

Hi,

I'm trying to get a value from a single key.

The following folders/keys are available in Consul.

groups/test/type
groups/test/name
groups/test/service
groups/other/type
groups/other/name
groups/other/service
...

With consul-template I just want to get the value from key "name". How do I do this?

This is what I have in my template:

{{ range $group, $items := tree "groups" | byKey }}
group {{ $group }} {
  name = {{ index $items "name" }}
}
{{ end }}

If I do the above I get the following output:

group test {
  name = {groups/test/name name foo 2650 2650 0 0 }
}
...

Most helpful comment

Hi @rgruyters

I think you'd need to save to a variable first, so you could query it, like this:

{{ range $group, $items := tree "groups" | byKey }}
group {{ $group }} {
{{ with index $items "name" }}
  name = {{ .Value }}
{{ end }}
}
{{ end }}

This is untested, but I think it should get you close. You'll likely need to fight with newline characters a bit to get the formatting to your liking.

All 2 comments

Hi @rgruyters

I think you'd need to save to a variable first, so you could query it, like this:

{{ range $group, $items := tree "groups" | byKey }}
group {{ $group }} {
{{ with index $items "name" }}
  name = {{ .Value }}
{{ end }}
}
{{ end }}

This is untested, but I think it should get you close. You'll likely need to fight with newline characters a bit to get the formatting to your liking.

Yep, that is working. Thanks!

Was this page helpful?
0 / 5 - 0 ratings