Is there any documentation on all the fields available in a nomad Task template?

In all the examples, I see references to the {{ .Address }} and {{ .Port }} fields in a “Service” struct (which according to the error message is a “dependency.HealthService”), but what other fields can I access from this object - and how?

Here is what I tried:

 {{- range service "server" }}
 SERVER_{{ .Meta.ordinal }}={{ .Address }}:{{ .Port }}
 {{ end -}}

And here is what I got:
Template failed: (dynamic): execute: template: :3:36: executing “” at <.Meta.ordinal>: can’t evaluate field Meta in type *dependency.HealthService.

The corresponding service definition is:

        service {
            name = "server"
            port = "admin"
            tags = [ "svr-${NOMAD_ALLOC_INDEX}" ]
            meta {
                ordinal = "${NOMAD_ALLOC_INDEX}"

As you can see, I need to create a set of ENV vars, one for each task registered to the service.

I have made that particular use-case work - using different means - by creating a separate service for each task; but that seemed heavyweight, when a single service with some metadata provides a much neater solution.

In a different use-case, I need to use the tag on the service.
I guess I should reference that something like: {{ .Tags }} or {{ index .Tags 0 }} (?)

So what I need is documentation on the actual structure of the HealthService type - and in fact all types accessible in the template section.

I had a quick scan of the source in GitHub, but didn’t find anything immediately.
(Should I be scanning nomad or consul or somewhere else?)

Any and all help gratefully received.

Kind Regards,

Hi @ntrevallyn,

The Nomad agent process uses consul-template as the library application which is responsible for rendering and updating templates. The documentation for the service template function you are calling can be found here.

The available fields on each service can be seen here in the source code which suggests you snippet of example template needs to look something like:

{{- range service "server" }}
  SERVER_{{ .ServiceMeta.ordinal }}={{ .Address }}:{{ .Port }}
{{ end -}}

jrasell and the Nomad team


Hi JRasell,

Thank You!
That is exactly what I needed.

  1. That the consul-template code/library is indeed being used;
  2. Doco on that, and the source code;
  3. info on functions that are supported - including spew_dump and friends.

Thank you once again.

Kind Regards,

1 Like