How to get unique configuration per job

Let’s say I had 10 counts of the demo http-echo service: https://github.com/hashicorp/http-echo

Normally that’d mean I have 10 instances of the simple Go server that echos the text hello world, but let’s say I want

each of these to echo a different letter of the alphabet from a list of 10 letters, and that if one of these goes down

say the one echoing the letter “C” that it is re-spawned accordingly. How might this be achieved?

You could do multiple tasks in an app, and use an environment variable to make the instances unique. Nomad will natively take care of respawning the service if it stops.

Note all services in a task will be run on the same host. If you want to have each instance run on a unique host, do 1 task per group.

job "hello-world" {
  region = "global"
  datacenters = ["dc1"]
  group "example" {
    count = 1
    task "A" {
      driver = "docker"
      config {
        image = "hashicorp/http-echo"
        args = [
          "-listen",
          ":5678",
          "-text",
          "hello ${LETTER}",
        ]
        port_map = {
          http = 5678
        }
      }
     env  {
        "LETTER" = "A"
      }
      resources {
        network {
          mbits = 10
          port  "http"{}
        }
      }
    }
    task "B" {
      driver = "docker"
      config {
        image = "hashicorp/http-echo"
        args = [
          "-listen",
          ":5678",
          "-text",
          "hello ${LETTER}",
        ]
        port_map = {
          http = 5678
        }
      }
     env  {
        "LETTER" = "B"
      }
      resources {
        network {
          mbits = 10
          port  "http"{}
        }
      }
    }
  }
}

Lovely stuff spuder, is there perhaps a way to capitalise on ${NOMAD_ALLOC_INDEX} and have it select from that index in env if it’s possible to use env as an array?

For instance

job "hello-world" {
  region = "global"
  datacenters = ["dc1"]
  group "example" {
    count = 2
    task "alphabet" {
      driver = "docker"
      config {
        image = "hashicorp/http-echo"
        args = [
          "-listen",
          ":5678",
          "-text",
          "hello ${LETTER}", // <-- some kind of syntax to do LETTER[NOMAD_ALLOC_INDEX]
        ]
        port_map = {
          http = 5678
        }
      }
     env  {
        "LETTERS" = ["A", "B"]
      }
      resources {
        network {
          mbits = 10
          port  "http"{}
        }
      }
    }
  }
}

the env block is for the process “environment”. It is not an arbitrary data structure. As such I doubt (am pretty sure) that you can’t use “env” like that. It would have to be “key=value” syntax.