Nomad Pack read variable inside of range

Hello,

I am working on building a Nomad Pack and am having troubles reading a global variable from inside a range.

Extract from variables.hcl:


variable "redis_database" {
     type = string
}

variable "redisdetails" {
  type = list(object({
    redis_host = string
    redis_password = string
  }))
  default = []
}

If I do this, it works fine:

/* [[ var "redis_database" . ]] is the database to use*/

[[ range var "networks" . ]] 

  template {
      data = <<<EOH
THE_REDIS_HOST=[[ .redis_host ]]
THE_REDIS_PASSWORD=[[ .redis_password ]]
EOH
}

[[ end ]]

But as soon as I reference a variable from the pack, it gives me an exotic error:

! Failed To Process Pack

    Error:   wrong type for value; expected parser.PackContextable; got map[string]interface {}
    Context: 
        - Filename: mypack/templates/_redisdb.tpl
        - Position: 7,17
        - Registry Name: <<local folder>>
        - Pack Name: mypack
        - Pack Ref: <<none>>
        - Pack Path: /home/davidtek/packs/mypack
        - Deployment Name: mypack




[[ range var "networks" . ]] 
/* [[ var "redis_database" . ]] is the database to use*/

  template {
      data = <<<EOH
THE_REDIS_HOST=[[ .redis_host ]]
THE_REDIS_PASSWORD=[[ .redis_password ]]
EOH
}

[[ end ]]

It would seem that invoking range has changed the scope of variables and I can no longer access the variables from the pack.

I tried using the name of the pack:

[[ var "redis_database" .mypack ]]

But this doesn’t work either.

How do I access the global variable scope from within the scope of a range ?

Thanks,

David

An example of a working loop, with range & “global” var. Might be other/better ways, but this was on the top of my head :wink:

variables.hcl

variable "test" {
  type = string

  default = "example"
}

variable "test_range" {
  type = list(object({
    one = string
    two = string
  }))

  default = [{
    one = "1"
    two = "2"
  }, {
    one = "3"
    two = "4"
  }]
}

job.nomad.tpl snippet

      [[ $something := var "test" . ]]
      [[- range $idx,$x := (var "test_range" .) ]]
      
      template {
        destination = "local/test-[[ $idx ]].txt"
        change_mode = "noop"
        data = <<-HEREDOC
          ONE=[[ $x.one ]]
          TWO=[[ $x.two ]]
          THREE=[[ $something ]]
          FOUR=[[ var "test" $ ]]
        HEREDOC
      }
      
      [[- end ]]

Output

      template {
        destination = "local/test-0.txt"
        change_mode = "noop"
        data        = <<-HEREDOC
          ONE=1
          TWO=2
          THREE=example
          FOUR=example
        HEREDOC
      }

      template {
        destination = "local/test-1.txt"
        change_mode = "noop"
        data        = <<-HEREDOC
          ONE=3
          TWO=4
          THREE=example
          FOUR=example
        HEREDOC
      }

I tried what you suggested and it worked perfectly.

I do wonder if there might be a better way than redefining my variables before the range.

Thanks!

David

Sorry; different account.

I show two different ways in the examples. The second one doesn’t require you to redeclare the variable (the “FOUR”). You use “$” instead of “.”

I completely missed FOUR.

Thank you!