Reputation: 135
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 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!
Upvotes: 0
Views: 653
Reputation: 141698
in "${fruits[@]}"; do
${...}
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:
for fruit in "$${fruits[@]}"; do
I typically store the script in a separate file. Check the following:
template {
dest = "local/script.sh"
# ./script.sh is relative to your _shell_ current working directory
# relative to the directory you are _running_ nomad command line tool.
# Separate file allows checking scripts easy with shellcheck.
data = file("./script.sh")
# You can't run the script without permissions.
perms = "755"
# Templates are consul-templated. Use unique delimiters.
# I typically use output from uuidgen
left_delimiter = "cee4faf9-64fb-45f2-af88-96e70902bc63"
right_delimiter = "82930c45-c997-4018-8d1d-50f9cc4fd420"
}
config {
# You do not know where is local/. Use env variable.
command = "${NOMAD_TASK_DIR}/script.sh"
}
Additionally, check your script with shellcheck. #!/bin/sh
has no arrays, arrays are a bash thing.
Upvotes: 1