Skip to content

Variables and Values

Variables and values provide ways to customize a job without altering the job's source code.

  • variables - like function parameters
  • local values - like local variables within a function

Example

example.nomad.hcl
variable "memory" {
    type = number
    description = "The amount of memory to allocate to the task"
    default = 4000
}

locals {
    default_name_prefix = "${var.project_name}-web"
    name_prefix = "${var.name_prefix != "" ? var.name_prefix : local.default_name_prefix}"
}

job "example" {
    name = "${local.name_prefix}-example"
    group "example" {
        task "example" {
            resources {
                memory = var.memory
            }

            config {
                args = [
                    "--memory, "${var.memory}"
                ]
            }
        }
    }
}
# Supplied individually as arguments
nomad job run -var="memory=1000" example.nomad.hcl

# Supplied as a file
nomad job run -var-file="prod.vars" example.nomad.hcl

# Environment variables
export NOMAD_VAR_memory=1000 # (1)
nomad job run example.nomad.hcl
  1. Environment variables are prefixed with NOMAD_VAR_ and are case sensitive

Resources


Last update: August 12, 2023
Created: August 12, 2023