Nomad Job Spec Environment Variable Best Practices

Hi @nickpoulos :wave:

I’m sorry to hear your team is having trouble migrating to Nomad. We appreciate you reaching out for help, and please keep raising these so we can help you and improve Nomad.

You certainly don’t have to manually read keys into env vars if their name match the desired variable result. You can instead range over the result of the ls function and set each key as an environment variable with the specific value.

Here’s an example based on what you provided:

job "env" {
  datacenters = ["dc1"]
  type        = "batch"

  group "env" {
    task "env" {
      driver = "docker"

      config {
        image   = "alpine:3.15"
        command = "/bin/sh"
        args    = ["-c", "env | grep APP"]
      }

      template {
        data        = <<EOF
{{ range ls "rtb" }}
{{ .Key }}={{ .Value }}
{{ end }}
EOF
        destination = "local/env"
        env         = true
      }
    }
  }
}

After setting some keys in Consul under the rtb/* path you will get an output like this:

We’re also working on Nomad Pack to provide Helm-like management and workflows. It’s still a work in progress, but we would love some early feedback. Here’s a tutorial on how to get started:

For linting, I’m not sure what kind of validations you are looking for, but have you looked at semgrep? Their HCL support is still in beta, but it seems to work quite well.

Here’s an example of a linting rule to check for the env vars from the job above:
https://semgrep.dev/s/L3jX

Is this the type of thing you are looking for?

3 Likes