Variable Interpolation

I apologize in advance if this question has been asked and answered already. I did a few searches and couldn’t seem to find any information on it.

I’m trying to embed a source or build variable into a variable, and am struggling to wrap my head around how to get that to work.

example:

variable singlesession {
  type = object({
    custom = object({
      type = object({
        osname = string
      })
    })
  })
  default = {
    singlesession = {
      custom = {
        osname = "windows-10-singlesession"
      }
    }
  }
}
source "azure-arm" "singlesession" {
  communicator   = "winrm"
  winrm_use_ssl  = false
  winrm_insecure = false
  winrm_use_ntlm = false
  winrm_timeout  = "10m"
  winrm_username = "packer"
  winrm_host     = "127.0.0.1"
}

build {
  name = "windows-10"
  source "azure-arm.singlesession" {
  }
   shared_image_gallery_destination = {
      image_name = "id-${var.${source.name}.custom.osname}"
}

I’m trying to build out a variables file that will work for multiple windows 10 builds that need to be customized based off of some sort of application suite set. I know, not great, but it’s what it is.

Maybe I’m trying to make it more complicated than it needs to be. If all else fails, I’ll just punt and declare/define separate variables for each of the builds in the source blocks.

Thanks in advance.

Came up with a workaround, just in case anyone else is in this situation.

Example:

source "null" "appsuite1" {
  communicator   = "winrm"
  winrm_username = local.winrm_username
  winrm_password = local.winrm_password
  winrm_host     = "127.0.0.1"
}
source "null" "appsuite2" {
  communicator   = "winrm"
  winrm_username = local.winrm_username
  winrm_password = local.winrm_password
  winrm_host     = "127.0.0.1"
}

build {
  name    = "windows-10"
  sources = ["null.appsuite1", "null.appsuite2"]

  provisioner "powershell" {
    inline = [
      "Set-Variable -Name vars -Value ('${jsonencode("${var.images}")}' | ConvertFrom-json)",
      "Write-Output $vars.${source.name}.custom.os_name",
    ]
  }
}

Returns:

    windows-10.null.appsuite1: windows10-appsuite1
    windows-10.null.appsuite2: windows10-appsuite2

Maybe there’s a better way to do it, but it seems to work. If anyone has a better idea, I’d love to hear it.