Hello,
I'm working with HashiCorp Nomad version 1.4.5 and facing an issue with a raw_exec task. The task is meant to loop through an array and print its elements. Here's my Nomad job configuration:
variable "datacenter" {
type = string
description = "Consul datacenter"
default = "cellebrite"
}
job "loop" {
datacenters = [var.datacenter]
type = "batch"
group "loop" {
count=2
constraint {
attribute = "${node.class}"
operator = "regexp"
value = "sdl_translation_engine.*"
}
task "prepare-sdl" {
driver = "raw_exec"
config {
command = "local/loop.sh"
}
template {
destination = "local/loop.sh"
data = <<EOH
#!/bin/sh
set -ex
# Declare an array
fruits=("apple" "banana" "cherry" "date" "fig")
# Loop through the array elements
for fruit in "${fruits[@]}"; do
echo "I like $fruit"
done
EOH
}
}
}
}
However, when I attempt to submit this job, I receive the following error:
Error getting job struct: Error parsing job file from sdl/loop_bash.nomad:
loop_bash.nomad:39,36-37: Invalid character; This character is not used within the language., and 1 other diagnostic(s)
Few notes:
- I have verified the syntax and structure of my Nomad job file.
- I've confirmed that the Bash script runs successfully when executed directly, without Nomad.
- I've confirmed that the job runs successfully when omitting the for loop.
I understand there is some issue with the nomad template trying to redner the loop but I'm unable to identify the root cause of this error. Any guidance or assistance in resolving this issue would be greatly appreciated.
Thank you!
${...}
are interpolated by Nomad https://developer.hashicorp.com/nomad/docs/runtime/interpolation and https://developer.hashicorp.com/nomad/docs/job-specification/hcl2/expressions#string-templates . You have to escape it. Do:I typically store the script in a separate file. Check the following:
Additionally, check your script with shellcheck.
#!/bin/sh
has no arrays, arrays are a bash thing.