Nomad 1.4 variable and iterate over `nomadVar` elements

I am trying out the Nomad 1.4-beta.1 and was experimenting with the variables section.

Using this as reference:

I created the variable named sample:

Is the following the most optimal way of consuming the key,value into the env?

As my Go-template fu is quite sub-par, I was wondering if there is a better alternative that the two step with and range.

      template {
        data = <<EOF
{{- with nomadVar "sample" }}
{{- range $kk, $vv := . }}
{{ $kk }}={{ $vv }}
{{- end }}
{{- end }}

        destination = "local/env"
        env         = true

@shantanugadgil :wave:

That template looks great. You need the wrapping with to guard the range function until the variable data is available, since consul-template uses multiple rendering passes over the template—so this is spot on. As Nomad 1.4 moves to GA, Nomad Variable support will make its way into consul-template mainline releases, which will expand access to Nomad Variables for templating outside of Nomad and also facilitate template development and troubleshooting without having to necessarily make a lot of round trip job submissions.

I did golf it down a little, and there is an interesting dance because these dependencies have real types.

{{ with nomadVar "sample" }}{{ .Parent.Items | sprig_toJson | parseJSON | toTOML }}{{end}}

The sprig_toJson | parseJSON shenanigans is a dirty cheat to remove the type information from the .Items object. I added GH issue with a proposed enhancement—Add a helper function to NomadVariableItems to get the Items as `map[string]interface{}` · Issue #14603 · hashicorp/nomad · GitHub

I’m excited that you’re already experimenting with Nomad Variables.


Charlie Voiselle (@angrycub)
Engineer - Nomad, HashiCorp

1 Like