Nomad HCL2 Function Examples

I’m just wondering if anyone could share some examples of how to use the new HCL2 functions in Nomad job definitions. The functions are well defined in documentation, but how they might be used within a Nomad job is a bit lacking. My first attempt to use them was within the template stanza, but it appears only Consul Template functions work within template. Just an example of how you might use a function elsewhere in the Nomad job would be really useful info.

Thanks!

Jeff

2 Likes

Hi @jeff.couvrette and thanks for the question.

One use case which I have personally had in the past is injecting Docker image tags within CI systems, where an application build triggers a downsteam deployment and passes it the newly produced version identifier to deploy.

A example job specification for how this might look would be as follows:

variable "image_version" {
 type    = string
 default = "latest"
}

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

  group "cache" {
    network {
      port "db" {
        to = 6379
      }
    }

    task "redis" {
      driver = "docker"

      config {
        image = format("redis:%s", var.image_version)
        ports = ["db"]
      }

      resources {
        cpu    = 500
        memory = 256
      }
    }
  }
}

The job can then be deployed to Nomad using a non-default image version using the nomad job run -var="image_version=4.0" example.nomad command.

Please let me know if you have any other questions or follow up.

Thanks,
jrasell and the Nomad team.

3 Likes

on the topic of functions, what is the correct way to use the timestamp and the formatdate functions??? (I have been trying so many various options with no success)

I am using version Nomad v1.0.2 (4c1d4fc6a5823ebc8c3e748daec7b4fda3f11037)

using the example job I added the CUR_DATE env to show what I have been trying.

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

  group "cache" {
    network {
      port "db" {
        to = 6379
      }
    }

    task "redis" {
      driver = "docker"

      config {
        image = "redis:3.2"

        ports = ["db"]
      }

      env {
        CUR_DATE = formatdate("YYYY-MM-DD", "${timestamp()}")
      }

      resources {
        cpu    = 500
        memory = 256
      }
    }
  }
}

@jrasell @angrycub any insights here?

As I understand it, the timestamp function does not exist in Nomad’s HCL2 interpreter. You could use Levant to template it in though.

There are two methods to accomplish your shortened date:
All Levant versions:

[[ index (timeNow | split "T") "_0" ]]

This works because you happen to be using YYYY-MM-DD as your desired date format. To have a more flexible scheme, you can extract more elements with split. This example transforms that date into MM/DD/YYYY:

[[- $split:=index (timeNow | split "T") -]]
[[- $date := index $split "_0" -]]
[[- $dSplit := split "-" $date -]]
[[- $YYYY := index $dSplit "_0" -]]
[[- $MM := index $dSplit "_1" -]]
[[- $DD := index $dSplit "_2" -]]
[[ printf "%s/%s/%s" $MM $DD $YYYY]]

In Levant 0.3.0, you have access to the sprig date functions. So you can use:

[[now | date "2006-01-02"]]

This uses the interesting golang date format strings, but is a much more condensed implementation. For the MM/DD/YYYY example it would be:

[[now | date "01/02/2006"]]

So there is a good harmony (even thought the weird date string is in the mix)

1 Like

Levant 0.3.0-beta1 FTW!!!

Just wondering why the HCL2 docs mention a function which doesn’t exist :frowning_face:

Apologies for that. The docs were pretty aggressively lifted and shifted from the Packer site. While trying to true them up for Nomad, some things were missed in the process. We’re working our way through them to verify that they are correct now and I expect some updates to be coming out soon.

1 Like

Thanks, I had assumed they were lifted from Terraform, as all of them seem to be near identical. :slight_smile: but all good now … in case the next time something doesn’t work, I won’t be too worried. :slight_smile:

There are still references to that nonexistant timestamp function in the docs, bottom of: