hashicorp / consul-template

Template rendering, notifier, and supervisor for @HashiCorp Consul and Vault data.
https://www.hashicorp.com/
Mozilla Public License 2.0
4.76k stars 783 forks source link

Group services by tags #1599

Closed tsiamer closed 2 years ago

tsiamer commented 2 years ago

Please note that the Consul Template issue tracker is reserved for bug reports and enhancements. For general usage questions, please use the Consul Community Portal or the Consul mailing list:

https://discuss.hashicorp.com/c/consul
https://groups.google.com/forum/#!forum/consul-tool

Please try to simplify the issue as much as possible and include all the details to replicate it. The shorter and simpler the bug is to reproduce the quicker it can be addressed. Thanks.

Consul Template version

Run consul-template -v to show the version. If you are not running the latest version, please upgrade before submitting an issue.

consul-template v0.29.0 (34ada7a)

Configuration:

I would like to group consul service by tags, using the below:

{{range services}}
  {{range $tag, $services := service .Name | byTag }}
    {{if or (eq $tag "Volkswagen") }}
      {{ range $services }}
      {{ $tag}}
      {{ .Name }}
{{ end }}{{ end }}{{end}}{{end}}

Output:

      Volkswagen
       Golf

      Volkswagen
       BUW

I d like it to be:

Volkswagen is the tag:

      Volkswagen

      Golf
      BUW

Thanks

eikenb commented 2 years ago

Hey @Siamert, thanks for the question.

Have you tried moving the {{ $tag }} out of the range block? Seems like this would fix that for you. Eg. something like...

{{range services}}
  {{range $tag, $services := service .Name | byTag }}
    {{if or (eq $tag "Volkswagen") }}
      {{ $tag}}
      {{ range $services }}
      {{ .Name }}
{{ end }}{{ end }}{{end}}{{end}}

Hope this helps!

tsiamer commented 2 years ago

Hi Eikenb,

Thank for the reply, same output i am getting:


     Volkswagen
      Golf

      Volkswagen
      BUW
eikenb commented 2 years ago

Sorry, I thought that inner range was the one with multiple entries instead of the middle one. That will take a different approach.

How something like this one? ...

{{- range $tag,$srv := services | byTag }}
{{ if (ne \$tag "Volkswagen") }}{{continue}}{{end}}
    {{ \$tag }}:
    {{- range $srv }}
        {{ .Name -}}
{{ end }}{{ end }}
tsiamer commented 2 years ago

Thank you it is not working getting below error, but it gives an idea to start with. parse: template: :2: function "continue" not defined

eikenb commented 2 years ago

Hmm... I see I accidentally left in some escapes, eg. the \ in \$tag. Remove those and it should work (or see below). Sorry about that.

{{- range $tag,$srv := services | byTag }}
{{ if (ne $tag "Volkswagen") }}{{continue}}{{end}}
    {{ $tag }}:
    {{- range $srv }}
        {{ .Name -}}
{{ end }}{{ end }}
tsiamer commented 2 years ago

Hmm... I see I accidentally left in some escapes, eg. the \ in \$tag. Remove those and it should work (or see below). Sorry about that.

{{- range $tag,$srv := services | byTag }}
{{ if (ne $tag "Volkswagen") }}{{continue}}{{end}}
    {{ $tag }}:
    {{- range $srv }}
        {{ .Name -}}
{{ end }}{{ end }}

Working thank you John.

eikenb commented 2 years ago

Glad I could help.

If in the future you have a question and I'm not able to answer it for some reason (work priorities or whatever) remember to try using our discuss forum as it has more eyes on it than these github issues and you may get more help, faster.

Good luck!