Using variables as part of other variables values

2.9k Views Asked by At

I am trying to do some kind of parameterizatiim inside one of the pkrvars.hcl files. I would like to have urls pointing to some resource to be using some other variables, like:

Lib_url = "https://lib-name-${version}`

Where version comes from other packer variables file. I can see that using variables is not possible this way. The question is - is it possible to use a variable/local value of a valie of some other variable in packer variables file?

1

There are 1 best solutions below

0
On

What you can do is have a variable which is configurable at runtime (either with a var-file, or -var or an env var PKG_VAR_var, see https://www.packer.io/guides/hcl/variables) and have other "variables" named locals which derive from this variable. See https://www.packer.io/docs/templates/hcl_templates/locals

an example

variables {
  version {
    type = string
    description = "OS version"
    default = "bullseye"
  }
}

locals {
  apt_url = "http://domain.tld/${var.version}"
  apt_key = "http://domain.tld/${var.version}.key"
}

Then in your build you use those variables with ${local.apt_url}