3

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?

Andrew
  • 47
  • 8
  • Yes, as long as you are using the HCL2 syntax (which it appears you are). Please expand on what you are trying to do here with the variable interpolation of another variable. Currently, `version` is not a valid namespace for a variable. – Matthew Schuchard Oct 12 '21 at 12:26
  • I have a lot of urls as variables. Somd urls are duplicated among variables so I wanted to introduce such variable so it can be changed in one place only when versiin is changed. Version in only an example – Andrew Oct 12 '21 at 14:49

1 Answers1

1

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}

daks
  • 843
  • 2
  • 7
  • 18