Hi, I was not sure whether this would fit the terraform or the nomad topic since I am using both…
My goal is to run a batch job in Nomad that only prints the content of a file to stdout, the file is a template file that either Terraform or Nomad should instantiate based on an external variable message
,
My issue is, that Nomad tasks run but the template is not expanded meaning, the task logs show.
hello ${message}
Instead of:
hello world!
These are the files used to reproduce the issue:
variables file vars.hcl, look var message
contains the value to expand the template file
helloworld_file = "./helloworld.txt.tpl"
nomad_api_url = "https://server.global.nomad:4646"
secret_id = "--REDACTED--"
nomad_cacert = "nomad-agent-ca.pem"
nomad_client_cert= "global-cli-nomad.pem"
nomad_client_key= "global-cli-nomad-key.pem"
message = "world!"
datacenter = "default"
the template file helloworld.txt.tpl
hello ${message}
terraform script main.tf
variable "helloworld_file" {
type = string
description = "local path file"
}
variable "nomad_api_url" {
type = string
}
variable "secret_id" {
type = string
}
variable "nomad_cacert" {
type = string
}
variable "nomad_client_cert" {
type = string
}
variable "nomad_client_key" {
type = string
}
variable "datacenter" {
type = string
}
variable "message" {
type = string
}
provider "nomad" {
address = var.nomad_api_url
secret_id = var.secret_id
ca_file = "${path.module}/provider_nomad_jobs/${var.nomad_cacert}"
cert_file = "${path.module}/provider_nomad_jobs/${var.nomad_client_cert}"
key_file = "${path.module}/provider_nomad_jobs/${var.nomad_client_key}"
}
resource "nomad_job" "test" {
hcl2 {
enabled = true
vars = {
"helloworld_file" = file("${var.helloworld_file}")
"datacenter" = var.datacenter
"message" = var.message
}
}
jobspec = file("${path.module}/provider_nomad_jobs/nomad-test.hcl")
detach = false
}
Nomad job provider_nomad_jobs/nomad-test.hcl
# Inject local files into the job ref https://discuss.hashicorp.com/t/passing-local-config-file-to-task/24598/3
variable "helloworld_file" {
type = string
description = "local path to file."
}
variable "datacenter" {
type = string
}
variable "message" {
type = string
}
job "nomad_test" {
datacenters = ["${var.datacenter}"]
type = "batch"
group "nomad_test" {
task "exec" {
driver = "raw_exec"
user = "root"
template {
destination = "local/hello.txt"
data = "${var.helloworld_file}"
}
config {
command = "bash"
args = ["-c", "cat $NOMAD_TASK_DIR/hello.txt"]
}
}
}
}
Before creating this ticket I tried this example Passing local config file to task - #3 by angrycub
Any idea what is wrong and how can I expand the message
var in the helloworld.txt.tpl template so Nomad tasks get the file with the content hello world!
?