Packer variable declaration issue


I’m trying to use packer using ansible playbook. I need to pass the variable to packer code via playbook so Im passing through extra arguments like below:

command: “packer build -var ‘parameter1=test’ -var ‘parameter2=test2’ myfile.hcl”

Now How I need to use it inside the myfile.hcl with these variable names.

When I try to use “{{ user parameter1 }}” getting error saying:

variable was passed in the command line but was not found in
known variables. To declare variable “parameter1”, place this block in one of
your .pkr files, such as variables.pkr.hcl

Please help me on this.

The error log says it all, just create a variable into your variable file, and set a default value, then when you pass it from Ansible to Packer, variable will exist and be overwritten by your value (else it will have default value you set)

Something like this :

variable "flavor" {
  type = string
  default = "strawberry"

thanks for your reply

1 Like