Can I set task resources using Consul key/value?


Is there a way to set values coming from Consul in task resources? something like that:

    task "resource-test" {
      driver = "docker"
      resources {
        cpu    = {{keyOrDefault "res/cpu" "200"}}
        memory = {{keyOrDefault "res/memory" "300"}}


Hi @Dgotlieb,

This is not possible as the resource information is needed for scheduling, whereas the template processes are invoked once an allocation has been scheduled and placed on a client. I would suggest looking at HCLv2 variables as a possible feature to use for setting these.

jrasell and the Nomad team

1 Like

Hi @jrasell,

Thanks a lot for your quick and informative answer.


Sounds like a jop for Nomad Pack / Levant. If you’re writing a task definition that needs to be templated out using data in Consul, before being submitted, maybe this would be the right approach?

Thanks @brucellino1!
We are already using Levant in templating, what I was looking for, is on a job (re)start event, the job will fetch the values from Consul.

According to ChatGPT :rofl: this is possible, so I was hoping it got it from somewhere:

Yes, it is possible to set the resources values in a Nomad job specification from Consul key-value using the Consul Template.

job "example-job" {
  datacenters = ["dc1"]
  type = "batch"

  group "example-group" {
    task "example-task" {
      driver = "docker"
      config {
        image = "my-docker-image"
        memory = {{ key "path/to/memory" }}
        cpu = {{ key "path/to/cpu" }}


1 Like

I see - you only consume the data at submit time, when you want to consume it also at restart time. Starting to sound like a job for the streaming API:

Maybe you could use a Consul event watch

to re-template the task definition and send it directly to the Nomad API, when the job is restarted?