Hello, I have question about template in separate file.
I have many jobs (150+) with this template:
job "example-app" {
datacenters = ["dc1"]
type = "service"
group "web" {
count = 1
task "server" {
driver = "docker"
.....
template {
data = <<-EOF
{{ with secret "kv/data/openobserve" }}
OPENOBSERVE_ADDRESS = "http://openobserve.service.consul:5080"
OPENOBSERVE_USERNAME = "{{ .Data.data.OPENOBSERVE_USERNAME }}"
OPENOBSERVE_PASSWORD = "{{ .Data.data.OPENOBSERVE_PASSWORD }}"
OPENOBSERVE_ORGANIZATION_NAME = "default"
OPENOBSERVE_SERVICE_NAME = {{env "NOMAD_JOB_NAME"}}
{{ end }}
EOF
destination = "task.env"
change_mode = "restart"
env = true
}
.....
Sometimes I need to change this template in all jobs
- add or delete some test envs.
Ideal solution for me - in all jobs put the link for the file with variables. Desired structure of the folder:
nomad
|jobs
|emails
|email-notify.nomad
|kafka
|kafka.nomad
|envs
|db.env.tpl
|kafka.env.tpl
|redis.env.tpl
Or more simply (job and envs in the same folder but not desired):
nomad
|jobs
|emails
|email-notify.nomad
|kafka.env.tpl
|redis.env.tpl
In documentation I found this:
job "docs" {
group "example" {
task "server" {
template {
source = "local/redis.conf.tpl"
destination = "local/redis.conf"
change_mode = "signal"
change_signal = "SIGINT"
}
}
}
}
I can’t understand - is source = "local/redis.conf.tpl"my local path or in the container?
I guess that in the container because got error:
Task hook failed: template: failed to read template: exit status 1: failed to open source file "/server/vars.env": open /server/vars.env: no such file or directory
So I don’t understand how to send my vars template to container.
In docs found Remote template
This example uses an artifact block to download an input template before passing it to the template engine:
artifact {
source = "https://example.com/file.yml.tpl"
destination = "local/file.yml.tpl"
}
template {
source = "local/file.yml.tpl"
destination = "local/file.yml"
}
But my env file is near my job! I don’t want to do web server for this …
I was pretty sure I had some nomad job in my cluster that loaded config files from file/* next to the .nomad.hcl job. I dug around but couldn’t find any including in my git history.
But I wasn’t entirely wrong, I have this crappy nomad-pack pack that handles some of my jobs that are more straightforward and IT is where I am loading config from a file instead of embedding it in the nomad hcl. Here is a snippet:
[[- /* Template Files */ -]]
[[- $job_dir := var "job_dir" . ]]
[[- range var "templates" . ]]
template {
[[- if $job_dir ]]
data = <<EOH
[[ fileContents (printf "%s/files/%s" $job_dir .src) ]]
EOH
[[- else ]]
data = "# ERROR: job_dir not set - cannot load [[ .src ]]"
[[- end ]]
[[- if .env ]]
destination = "secrets/[[ .src ]]"
env = true
[[- else if .vault ]]
destination = "secrets/[[ trimSuffix ".tpl" .src ]]"
[[- else ]]
destination = "local/[[ .src ]]"
[[- end ]]
[[- if .change_mode ]]
change_mode = "[[ .change_mode ]]"
[[- else ]]
change_mode = "restart"
[[- end ]]
}
[[- end ]]
you can see the whole pack at nomad-pack-registry/munchbox-service/templates/job.nomad.tpl at 5ef26d7f974117a960c9da812c05e492e3a7a703 · afreidah/nomad-pack-registry · GitHub around like 390.
not sure if that is useful to you or not.
here is an example job I have that uses that pack I made and loads a config from files/registry-config.yaml and puts it at /local/config.yaml in the container.
# -------------------------------------------------------------------------------
# Docker Registry — Container Image Storage with UI
#
# Project: Munchbox / Author: Alex Freidah
#
# Docker Registry v2 for storing and distributing container images within the
# cluster. Includes web UI for browsing repositories and managing images.
# -------------------------------------------------------------------------------
# --- Core job configuration ---
name = "registry"
type = "service"
image = "registry:3.1.1"
port = 5000
static_port = 5000
host_network = true
node = "stabler"
size = "large"
# --- Storage ---
storage = "ephemeral"
volumes = [
"/mnt/gdrive/munchbox-data/registry:/var/lib/registry"
]
# --- Traefik routing ---
traefik = true
traefik_host = "registry.munchbox.cc"
# --- Health check ---
health_path = "/v2/"
# --- Environment (OTEL tracing to Tempo) ---
env = {
OTEL_EXPORTER_OTLP_ENDPOINT = "http://tempo.service.consul:4318"
OTEL_EXPORTER_OTLP_INSECURE = "true"
}
# --- Container arguments ---
args = ["serve", "/local/config.yml"]
# --- Configuration templates ---
templates = [
{ src = "registry-config.yml", dest = "/local/config.yml", vault = false }
]
# --- Service tags ---
tags = [
"docker",
"registry",
"infrastructure",
]
# --- Termination ---
kill_timeout = "30s"
You can see the full directory with the registry and registry-ui jobs using the pack and the two config files in files/ (one for each of those jobs) in my homelab repo: munchbox/nomad/jobs/infrastructure/registry at main · afreidah/munchbox · GitHub
What I am starting to do now is to instead put the config in consul k/v (provisioned into consul by terragrunt orchestrating terraform module(s)) and read the values into the nomad job. Something like this should work:
job "example" {
datacenters = ["dc1"]
type = "service"
group "app" {
task "app" {
driver = "docker"
config {
image = "example/app:1.2.3"
args = ["--rules-dir=/etc/app/rules"]
volumes = ["local/rules:/etc/app/rules"]
}
# --- One KV key per rule group under app/rules/; set membership is whatever exists ---
template {
destination = "local/rules/rules.yml"
change_mode = "signal"
change_signal = "SIGHUP"
data = <<-EOT
groups:
{{- range tree "app/rules" }}
{{ .Value -}}
{{ end }}
EOT
}
resources {
cpu = 100
memory = 128
}
}
}
}