hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
14.99k stars 3.33k forks source link

Not defining a default value causes "packer build" to crash #8941

Closed deric4 closed 4 years ago

deric4 commented 4 years ago

Overview of the Issue

Was kicking the tires on the hcl2 support so not sure if this actually a bug, but it seems that defining variables w/o a default value in required.pkr.hcl and setting the value in a separate file i.e. myvars.pkrvars causes packer build to crash

Reproduction Steps

Steps to reproduce this issue

packer build .

Packer version

From packer version

$ packer version
Packer v1.5.4

Simplified Packer Buildfile

If the file is longer than a few dozen lines, please include the URL to the gist of the log or use the Github detailed format instead of posting it directly in the issue.

# required.pkr.hcl

variable "ami_name" {
    description = "my ami name"
}

variable "vpc_id" {
   default = ""
   description = "the builder instance vpc id"
}

variable "subnet_id" {
 default = ""
 type = string
 description = "the builder instance subnet-id"
}

# myvars.pkrvars

ami_name = "mysupercoolami"
vpc_id = "vpc-xxxxxx"
subnet_id = "subnet-xxxxxxx"

# sources.pkr.hcl
source "amazon-ebs" "ubuntu1604" {
  ami_name = var.ami_name
  region = "us-east-1"
  instance_type = "t2.small"
  source_ami_filter {
    filters {
      virtualization-type = "hvm"
      name = "ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"
      root-device-type = "ebs"
    }
    owners = [ "099720109477" ]
    most_recent = true
  }
  communicator = "ssh"
  ssh_username = "ubuntu"
  vpc_id = var.vpc_id
  subnet_id = var.subnet_id
  force_deregister = true
}

# build
build {
  sources = [
    "source.amazon-ebs.ubuntu1604"
  ]
}

Operating system and Environment details

OS, Architecture, and any other information you can provide about the environment.

$ uname -a
Darwin 19.3.0 Darwin Kernel Version 19.3.0: Thu Jan  9 20:58:23 PST 2020; root:xnu-6153.81.5~1/RELEASE_X86_64 x86_64

Log Fragments and crash.log files

Include appropriate log fragments. If the log is longer than a few dozen lines, please include the URL to the gist of the log or use the Github detailed format instead of posting it directly in the issue.

Set the env var PACKER_LOG=1 for maximum log detail.

Logs:

https://gist.github.com/deric4/2816ef87ccc67a2aa2fdb8553a5dcd11

Some Other Observations

when I set a default value in required.pkr.hcl, it just gave a normal error. Based on #8882 , I thought this would work but not a big deal...

# required.pkr.hcl
variable "ami_name" {
   default = ""
   description = "my ami name"

https://gist.github.com/deric4/2816ef87ccc67a2aa2fdb8553a5dcd11#file-error_with_empty_default-log

It does work however if I rename myvars.pkvars -> myvars.auto.pkvars (with the default set in required.pkr.hcl)

azr commented 4 years ago

Hey @deric4, sorry to hear you had that bug; it was fixed on master and the latest nightly binaries should work with your case. Can you please tell us if that's the case ? Thanks !

deric4 commented 4 years ago

Thanks @azr for the quick follow up! I searched recent issues and missed that one: I no longer see the panic šŸ˜„ . Should it still be an error to not define a default in required.pkr.hcl while setting it in myvars.pkrvars.hcl though?

#myvars.pkrvars.hcl
ami_name = "mycoolami"

#required.pkr.hcl
variable "ami_name" {
   description = "the name of the ami"
}
Error Log ```bash $ ./packer_darwin_amd64 build . 2020/03/24 08:35:44 [INFO] Packer version: 1.5.5-dev (d068430abf13c9d9dfb1a74e3cf3fb648091841e) [go1.13.8 darwin amd64] 2020/03/24 08:35:44 Checking 'PACKER_CONFIG' for a config file path 2020/03/24 08:35:44 'PACKER_CONFIG' not set; checking the default config file path 2020/03/24 08:35:44 Attempting to open config file: /Users//.packerconfig 2020/03/24 08:35:44 [WARN] Config file doesn't exist: /Users//.packerconfig 2020/03/24 08:35:44 Setting cache directory: /Users//projects/common-libs/packer-ubuntu1604/packer_cache A used variable must be set or have a default value; see https://packer.io/docs/configuration/from-1.5/syntax.html for details. Error: Unset variable "ami_name" 2020/03/24 08:35:44 Build debug mode: false 2020/03/24 08:35:44 Force build: false 2020/03/24 08:35:44 On error: 2020/03/24 08:35:44 Waiting on builds to complete... ==> Builds finished but no artifacts were created. A used variable must be set or have a default value; see 2020/03/24 08:35:44 [INFO] (telemetry) Finalizing. https://packer.io/docs/configuration/from-1.5/syntax.html for details. ==> Builds finished but no artifacts were created. 2020/03/24 08:35:45 waiting for all plugin processes to complete... ```

It works when I convert my myvars.pkrvars.hcl to the auto format: myvars.auto.pkrvars.hcl

edit: basically trying to understand/implement what you guys were discussing regarding -var-file here https://github.com/hashicorp/packer/pull/8882#issuecomment-598656653

azr commented 4 years ago

Hey @deric4 sure šŸ˜„ ! Nice that you don't see the panic šŸ‘ . I'll keep that issue around in case someone has it again. We'll close it when we release.

Packer will only 'auto detect' var files ending with .auto.pkrvars.hcl or .auto.pkrvars.json, if you want to use myvars.pkrvars.hcl you have to pass it from the -var-file option.

azr commented 4 years ago

Okay, closing this issue as it was fixed in 1.5.5 !

ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ā³. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.