How do I use value from consul kv in task config?

Hello!

I have a job for which the docker image revision is stored in consul. I can’t figure out how to get it to work.

Hello!

I have a job for which the docker image revision is stored in consul. I can’t figure out how to get it to work.

job "test-job" {
  datacenters = ["nomad"]
  type = "service"
  group "group" {
    count = 1
    task "task" {
      driver = "docker"
      config {
          image = "mysql:{{key "some/consul/path/to/Revision"}}"
      }     
    }
  }
}

When I run this job I get the following error:


Parse Error

input.hcl:9,33-37: Missing newline after argument; An argument definition must end with a newline.

It’s clearly unhappy about the quotes within quotes.

So I tried this,

job "test-job" {
  datacenters = ["nomad"]
  type = "service"
  group "group" {
    count = 1
    task "task" {
      driver = "docker"

      config {
          image = "mysql:${env.Revision}"
      }

      template {
        data = <<EOH
        Revision = "{{key "some/consul/path/to/Revision"}}"
EOH
        destination = "secrets/file.env"
        env         = true
      }

     
    }
  }
}

Suddenly I can read from Consul and use it for the image revision, but this seems convoluted. What’s more if I want to use vault ( secret instead of key ) what happens ? If there is a secret, is it going to be exposed to the user space environment when it’s part of the job ?

I understand the syntax is different inside a template stanza, but I don’t understand how to do the consul lookup in the image field ( or auth or any other field inside of config ) .

Can someone point me in the right direction please?

Thanks,

David

hi,

this is my example:

...
  template {
    data = <<EOH
{{key "nomad/backoffice/environment"}}
EOH
    destination = "local/env"
    env         = true
  }
...

The path on Consul is: kv/nomad/backoffice/environment

and the content of environment looks like:

FOO="bar"
MTA="localhost"

You may set env = false and check, if the file was creates, as expected.

I wasted also a lot of times, as it depends, how the entries are saved in Consul. I had before YAML syntax, and also some JSON. I would say: make your image path to a valid one, so that the container starts, and than have a look on the file … you you get then the right syntax … change it back.

1 Like