Packer Not reading variables from file

Hello Packer Community,
I am running into an issue where packer isnt reading variables from file. I created a pkr.hcl file to store credentials as follows
credentials.pkr.hcl

proxmox_api_url="https://prox.internal.lan:8006/api2/json"
proxmox_api_token_id = "admin"
proxmox_api_token_secret = "Pw"

My main pkr.hcl file

variable "proxmox_api_url" {
    type = string
}
variable "proxmox_api_token_id" {
    type = string
}
variable "proxmox_api_token_secret" {
    type = string
    sensitive = true
}

source "proxmox" "ubnt-2204" {
    proxmox_url = "${var.proxmox_api_url}"
    username = "${proxmox_api_token_id}"
    password = "${proxmox_api_token_secret}"
...
}

Everything is defined as it should and when I run the command. It doesnt read 2 of the variables.

packer validate -var-file="credentials.pkr.hcl" ubnt-2204/ubnt-2204.pkr.hcl 

Error: Unknown variable

  on ubnt-2204/ubnt-2204.pkr.hcl line 14:
  (source code not available)

There is no variable named "proxmox_api_token_id".

Error: Unknown variable

  on ubnt-2204/ubnt-2204.pkr.hcl line 15:
  (source code not available)

There is no variable named "proxmox_api_token_secret".

Its reading proxmox_api_url but not the other 2. Any guidance as to why its reading selectively would be greatly appreciated

Hi @Riprock,

Your variables don’t exist from Packer’s perspective since they’re not prefixed with var.

The error is misleading a bit, this is due to the way Packer exposes its context to the HCL library for evaluating those, i.e. variables are placed under a var root, so in this case if you’re referencing a variable without that prefix, it will tell you it doesn’t exist.

In your case, this should do the trick:

[...]
source "proxmox" "ubnt-2204" {
    proxmox_url = "${var.proxmox_api_url}"
    username = "${var.proxmox_api_token_id}"
    password = "${var.proxmox_api_token_secret}"
...
}

Hope that helps!

Yes it does. Im dumb for forgetting to add the var. before the other two variable names. Thank you so much!