How to pass snippets of template between jobs

Hi,

I’m looking for a Nomad-native (i.e. without Consul or Terraform) way to pass snippets of config between jobs, which can then be interpreted by the template block to have Nomad service discovery variables filled in.

I’ve tried declaring them as a local variable using heredoc formatting and then assigning the variable to a tag on the source job. The target job can then see the tag and pull it into it’s own template block, but any variables in the transferred code snippet don’t get interpolated as the templating process only makes one pass of the template.

source job:

locals {
  snippet = <<EOH
snippet=some custom per-job config here (can not be templated) {
  reverse_proxy {{ range nomadService "source-job" }}{{ .Address }}:{{ .Port }} {{ end }}
}
some more custom per-job config here
EOH
...
job "source-job"
  group "test" {
    task "test" {
      tags = [ local.snippet ]

target job

            template {
                data = <<EOH
{{ range $tag, $services := nomadServices | byTag }}
  {{ range $services }}
    {{ if in $tag  "snippet=" }}
      {{- $tag | trimPrefix "snippet=" }}
    {{ end }}
  {{ end }} 
{{ end }}

template renders as

some custom per-job config here (can not be templated) {
  reverse_proxy {{ range nomadService "source-job" }}{{ .Address }}:{{ .Port }} {{ end }}
}
some more custom per-job config here

rather than

some custom per-job config here (can not be templated) {
  reverse_proxy 192.168.1.1:80 192.168.1.3:80
}
some more custom per-job config here

Is there a way to make this work or a simpler way to do this that i’ve missed? I’m trying to do this to combine config from multiple source jobs, but the snippet is too complex and varied between each source job to have a standardised template.

I’ve almost figured a way around this, I’m generating the first pass of the template in a prestart sidecar job which then gets put into the alloc/ dir as a file. The main target task can then use this file as source for another pass of the template.

The only thing i’m missing now is that source doesn’t watch for changes in the underlying file, so further updates to the first pass template don’t get pulled in by the main job

Feature request: parse sub-templates dynamically · Issue #1402 · hashicorp/consul-template · GitHub would solve this a lot more simply