diff --git a/command/hcl2_upgrade.go b/command/hcl2_upgrade.go index e149e1af0..3df4fb452 100644 --- a/command/hcl2_upgrade.go +++ b/command/hcl2_upgrade.go @@ -870,6 +870,9 @@ func (p *VariableParser) Parse(tpl *template.Template) error { p.localsOut = []byte{} } + if len(tpl.Variables) == 0 { + tpl.Variables = make(map[string]*template.Variable) + } // JSON supports variable declaration via var-files. // User variables that might be defined in a var-file // but not in the actual JSON template should be accounted for. diff --git a/command/hcl2_upgrade_test.go b/command/hcl2_upgrade_test.go index 45da6a1cd..d7899fe6e 100644 --- a/command/hcl2_upgrade_test.go +++ b/command/hcl2_upgrade_test.go @@ -34,6 +34,7 @@ func Test_hcl2_upgrade(t *testing.T) { {folder: "variables-with-variables", flags: []string{}}, {folder: "complete-variables-with-template-engine", flags: []string{}}, {folder: "undeclared-variables", flags: []string{}, exitCode: 0}, + {folder: "varfile-with-no-variables-block", flags: []string{}, exitCode: 0}, } for _, tc := range tc { diff --git a/command/test-fixtures/hcl2_upgrade/varfile-with-no-variables-block/expected.pkr.hcl b/command/test-fixtures/hcl2_upgrade/varfile-with-no-variables-block/expected.pkr.hcl new file mode 100644 index 000000000..c2e5767f8 --- /dev/null +++ b/command/test-fixtures/hcl2_upgrade/varfile-with-no-variables-block/expected.pkr.hcl @@ -0,0 +1,33 @@ + +variable "ssh_host" { + type = string +} + +variable "ssh_password" { + type = string +} + +variable "ssh_username" { + type = string +} + +variable "version_tag" { + type = string +} + +source "null" "autogenerated_1" { + communicator = "ssh" + ssh_host = "${var.ssh_host}" + ssh_password = "${var.ssh_password}" + ssh_username = "${var.ssh_username}" +} + +build { + sources = ["source.null.autogenerated_1"] + + provisioner "shell-local" { + inline = ["echo ${var.version_tag} > provision.txt"] + pause_before = "20s" + } + +} diff --git a/command/test-fixtures/hcl2_upgrade/varfile-with-no-variables-block/input.json b/command/test-fixtures/hcl2_upgrade/varfile-with-no-variables-block/input.json new file mode 100644 index 000000000..bb901d095 --- /dev/null +++ b/command/test-fixtures/hcl2_upgrade/varfile-with-no-variables-block/input.json @@ -0,0 +1,18 @@ +{ + "builders": [ + { + "type": "null", + "communicator": "ssh", + "ssh_host": "{{ user `ssh_host` }}", + "ssh_password": "{{ user `ssh_password` }}", + "ssh_username": "{{ user `ssh_username` }}" + } + ], + "provisioners": [ + { + "pause_before": "20s", + "type": "shell-local", + "inline": [ "echo {{ user `version_tag` }} > provision.txt" ] + } + ] +} diff --git a/command/test-fixtures/hcl2_upgrade/varfile-with-no-variables-block/variables.json b/command/test-fixtures/hcl2_upgrade/varfile-with-no-variables-block/variables.json new file mode 100644 index 000000000..4e4b93c64 --- /dev/null +++ b/command/test-fixtures/hcl2_upgrade/varfile-with-no-variables-block/variables.json @@ -0,0 +1,6 @@ +{ + "version_tag": "1.0.0", + "ssh_host": "localhost", + "ssh_username": "packer", + "ssh_password": "packer" +}