Introduction
Variables are a great way to minimize the complexity and improve the readability of your jobs; generally speaking, you would want to specify a default value to be used in case none are provided when the job is run. Nomad also allows for complex type variables, such as maps, which also accept input variables when the job is run.
A variable declared in your jobspec can be as simple as the example below
variable "image1" {
type = string
default = "1"
}
Using a value other than the default when running your job is achieved by adding the -var option when running your job.
nomad job run -var="image1=3" ./myjob.hcl
Note: The -var option can be added multiple times, for each variable you've declared.
Variable maps are a bit different, in that they contain more than one entry for the variable.
variable "test" {
type = map(string)
default = {
ex1 = "4"
ex2 = "5"
ex3 = null
ex4 = null
}
}
Expected Outcome
By the end of this article, you will be able to declare variable maps in your jobs and will be able to override the defaults when running your job.
Prerequisites
First, you'll want to create a jobspec which uses a variable of type map, below is an example that can be used/modified for your use case.
# myjob2.hcl
variable "test" {
type = map(string)
default = {
ex1 = "4"
ex2 = "5"
ex3 = null
ex4 = null
}
}
job "myjob" {
datacenters = ["dc1"]
type = "service"
group "grp1" {
count = 1
task "task1" {
driver = "docker"
config {
image = "redis:${var.test["ex1"]}"
}
}
}
group "grp2" {
count = 1
task "task2" {
driver = "docker"
config {
image = "redis:${var.test["ex2"]}"
}
}
}
}
Procedure
- Run your job and include the -var option so that ex1 and ex2 will use 7 and 6 respectively
$ nomad job run -var='test={"ex1":"7","ex2":"6","ex3":null,"ex4":null}' ./myjob2.hcl
-
Next, view the running allocations to check the Redis version used for the deployment.
$ nomad status myjob
ID = myjob
Name = myjob
Submit Date = 2023-10-24T16:54:27Z
Type = service
Priority = 50
Datacenters = dc1
Namespace = default
Node Pool = default
Status = running
Periodic = false
Parameterized = false
Summary
Task Group Queued Starting Running Failed Complete Lost Unknown
grp1 0 0 1 0 0 0 0
grp2 0 0 1 0 0 0 0
Latest Deployment
ID = 114d7c40
Status = successful
Description = Deployment completed successfully
Deployed
Task Group Desired Placed Healthy Unhealthy Progress Deadline
grp1 1 1 1 0 2023-10-24T17:04:43Z
grp2 1 1 1 0 2023-10-24T17:04:49Z
Allocations
ID Node ID Task Group Version Desired Status Created Modified
4cb2b3ea 21915867 grp2 0 run running 4m11s ago 3m49s ago
bd7f533c 21915867 grp1 0 run running 4m11s ago 3m54s ago -
Finally, view the allocation logs for each task (The redis versions should be 6 & 7 respectively)
$ nomad alloc logs bd7f533c task1
1:C 24 Oct 2023 16:54:33.336 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:C 24 Oct 2023 16:54:33.337 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 24 Oct 2023 16:54:33.337 * Redis version=7.2.2, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 24 Oct 2023 16:54:33.337 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 24 Oct 2023 16:54:33.337 * monotonic clock: POSIX clock_gettime
1:M 24 Oct 2023 16:54:33.338 * Running mode=standalone, port=6379.
1:M 24 Oct 2023 16:54:33.338 * Server initialized
1:M 24 Oct 2023 16:54:33.338 * Ready to accept connections tcp
$ nomad alloc logs 4cb2b3ea task2
1:C 24 Oct 2023 16:54:39.070 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 24 Oct 2023 16:54:39.070 # Redis version=6.2.14, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 24 Oct 2023 16:54:39.070 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 24 Oct 2023 16:54:39.070 * monotonic clock: POSIX clock_gettime
1:M 24 Oct 2023 16:54:39.071 * Running mode=standalone, port=6379.
1:M 24 Oct 2023 16:54:39.071 # Server initialized
1:M 24 Oct 2023 16:54:39.071 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 24 Oct 2023 16:54:39.072 * Ready to accept connections
Additional Information
-
You can find additional information about HCL2 variables and their usage here.