Hi all,
I am working on a Nomad job created via the Nomad Terraform provider, because I want to reference other Terraformed resources.
My problem seems similar to
In this case, the suggestion was to store the data in Consul and read it later. If this is the only way to do what I want, so be it, although it seems somewhat inelegant.
Job details
For context, this is a Nomad job to deploy Grafana Loki, using a Digital Ocean space as s3 storage.
I have three files:
main.tf
has all of the terraform resourcesloki.nomad
is a Nomad job definitionloki.yml.tpl
is a Nomad template which produces the Loki configuration file, and is passed as input to atemplate
block in the Job’stask
stanza
The problem I’m having is passing data from Terraform, through the Nomad job definition, into the Nomad template:
Template failed: (dynamic): parse: template: :23: function "var" not defined
The yml template is as follows:
auth_enabled: false
server:
http_listen_port: 3100
grpc_listen_port: 9096
memberlist:
join_members:
- loki-http-server
schema_config:
configs:
- from: 2022-01-01
store: boltdb-shipper
object_store: s3
schema: v11
index:
prefix: index_
period: 24h
common:
path_prefix: local/
replication_factor: 1
storage:
s3:
endpoint: {{ var.s3_endpoint }}
bucketnames: {{ var.logs_bucket }}
access_key_id: {{ var.access_key }}
secret_access_key: {{ var.secret_key }}
s3forcepathstyle: true
ring:
kvstore:
store: consul
ruler:
storage:
s3:
bucketnames: {{ var.logs_bucket }}
The Nomad job which uses it is as follows:
task "server" {
driver = "exec"
config {
command = "loki"
args = [
"-config.file=local/loki.yml"
]
}
resources {
cpu = 128
memory = 200
}
template {
data = file("loki.yml.tpl")
destination = "local/loki.yml"
change_mode = "restart"
}
artifact {
source = "https://github.com/grafana/loki/releases/download/v2.6.0/loki-linux-arm64.zip"
options { # checksum depends on the cpu arch
}
destination = "local/loki"
mode = "file"
}
}
while the Nomad job resource in Terraform is:
resource "nomad_job" "loki" {
jobspec = file("${path.module}/loki.nomad")
depends_on = [digitalocean_spaces_bucket.logs]
hcl2 {
enabled = true
allow_fs = true
vars = {
"logs_bucket" = digitalocean_spaces_bucket.logs.name,
"s3_endpoint" = "https://${digitalocean_spaces_bucket.logs.region}.digitaloceanspaces.com",
"access_key" = jsondecode(data.vault_kv_secret_v2.digitalocean.data_json)["spaces_key"]
"secret_key" = jsondecode(data.vault_kv_secret_v2.digitalocean.data_json)["spaces_secret"]
}
}
purge_on_destroy = true
detach = false
}
While I can read the Vault secrets in the Nomad template, I don’t see how I can get the logs_bucket
and s3_endpoint
from anything else but the Terraform state.
This is somewhat besides the point though, since even without Terraform, I want to be able to ass Nomad declared variables to Nomad templates.
So - how do I pass Nomad variables declared in the Nomad job definition to templates in that job definition?
From what the error is telling me there is no var
function in the Consul template – is the only way to get Nomad variables into a template via some way that doesn’t actually use Nomad variables?
Perhaps I’m going about this all wrong, so I would appreciate any advice on the matter.
Cheers!
Bruce