How to use templates

Hi, i am trying to do something like this in the tags from the services section:

“traefik.http.routers.test.rule=Host(auth.${element(split(".", NOMAD_DC),1)}.test.live)”,

but does not seem that NOMAD_DC is interpolated

Anyone any ideas ?

Hi @cberescu. Nomad runtime interpolation is different from HCL2 job specification parsing meaning the above type of functionality is not possible. Interpolation can only substitute values, not perform additional functionality.

The “NOMAD_DC” interpolation value, however, will only ever represent a single datacenter value as each Nomad client can only be configured to exist within a single datacenter. This therefore means that you do not need to perform the element and split functions in order to grab the datacenter value and the example below should be sufficient.

“traefik.http.routers.test.rule=Host(auth.${NOMAD_DC}.test.live)”

Thanks,
jrasell and the Nomad team

Thanks for the answer @jrasell .
What you suggested unfortunately does not help me in my use case.
Do you know any way to do what i described ? Can i maybe using locals or something else?
Also, is there a way to use Consul template in the tags or use data from consul in the tags section of service ?

Thank you.

Can i maybe using locals or something else?

HCL2 locals and HCL2 features generally are only available for use during job specification parsing which occurs locally before the rendered job specification is sent to the Nomad servers for scheduling.

Consul template in the tags or use data from consul in the tags section of service

It could be possible to use a template stanza to read data from Consul and then set task environment variables which could then be interpolated within service tags, although I have not tested this possibility. consul-template cannot be used directly within the service tags, this only supports interpolation.

Thanks,
jrasell and the Nomad team

Thanks for the advice, it works great.

I will let sample code here in case someone will be interested to do something like that

template {
			data = <<EOH
		{{ $node_dc := env "NOMAD_DC" }}	
		# Lines starting with a # are ignored
		{{ $found := "0" }}	
		{{range $index, $service := service "something" }}
			{{if eq $service.ServiceMeta.ndc $node_dc}}
				{{ $found = "1" }}	
				AUTHHOST="http://{{$service.Address}}:{{$service.Port}}/"
			{{end}}
		{{end}}
		{{if ne $found "1"}}
			AUTHHOST="http://somehost/"
		{{end}}
		EOH

and in the tags works like this :

"traefik.http.middlewares.test.forwardauth.address=${AUTHHOST}",