Passing enviroments variables into nomad job

my nomad job will run on various machines and will be based on environment variables set in the current environment. I’m trying to solve it with templates (and it’s not working), but I’m open to any suggestions.

Here’s my job config:

job "test" {
  datacenters = ["dc1"]

  type = "batch"

  group "test-group" {
     restart {
       attempts = 0
     }

     reschedule {
       attempts  = 0
       unlimited = false
     }
      

    task "test-task" {
      driver = "docker"

      template {
        data = <<EOH
          TEST_VAR="{{ env "TEST_VAR" }}"
        EOH

        change_mode = "restart"
        destination = "secrets/file.env"
        env         = true
      }

      config {
        image = "test:test"
      }

     
    }
  }
}

and my docker file:

FROM alpine

ENTRYPOINT echo testing $TEST_VAR

as I said, this isn’t working, job is running successfully, but I get only testing in the log.

Any idea?

Thanks!

hi
In my opinion, it is recommended to remove the indentation of what goes inside “EOH”.

It looks like below

 task "test-task" {
      driver = "docker"

      template {
        data = <<EOH
TEST_VAR="{{ env "TEST_VAR" }}"
        EOH

        change_mode = "restart"
        destination = "secrets/file.env"
        env         = true
      }

      config {
        image = "test:test"
      }

I will pray for this to be.

Hi @chiptus,

What is the value of TEST_VAR expected to be and how is this being configured and populated on the clients where this workload is running?

If you’re looking to add custom env vars per job, you can use the env block rather than the template block.

Thanks,
jrasell and the Nomad team

@swbs90 thanks for the suggestion but it didn’t help.

@jrasell the env vars are decided by the environment, not by the job. so we want to run the same job file on different environments, and each should get it env vars from the environment itself, not from the job file. That’s why I went with template, I didn’t find how to do it with the env block.

TEST_VAR is just a sample var, I’m just trying to find out how to make it work, then I’ll apply it to the final job file.

I run like this:

export TEST_VAR=test
nomad job run test.nomad

Thanks for the replies!

Hi @chiptus and thanks for the additional details.

The template block is rendered on the client where the allocation is run and not the local machine where the nomad job run command is executed. This would explain why you are seeing the results and problem you currently have.

In order to achieve the results you are looking for I would use a combination of HCL2 variables and the env block.

The job specification will detail an input variable, and then reference the input within the env block in order to export this to the tasks environment. In order to trigger the job with a custom variable, you can use the nomad job run -var='env_var=my-custom-var' file.nomad

variable "env_var" {}

job "test" {
  datacenters = ["dc1"]

  type = "batch"

  group "test-group" {
     restart {
       attempts = 0
     }

     reschedule {
       attempts  = 0
       unlimited = false
     }
      

    task "test-task" {
      driver = "docker"

      env {
        TEST_VAR = var.env_var
      }

      config {
        image = "test:test"
      }
    }
  }
}

Thanks,
jrasell and the Nomad team

Thanks for the reply @jrasell

We’re using the API to deploy instead of the cli, and if I understand the code right, variables are only available from the cli. I was hoping for the template stanza to be able to query the current environment to get those env variables.

Thanks again, I’ll continue to explore the options.