Converted json to hcl2 -> a million errors

Json worked, hcl2 doesn’t. I’m completely new to Packer and I need a way to replace kickstart injection on kvm based hypervisor to vSphere, and I can’t use pxe boot.

I stared working with json because I didn’t realize it was deprecated and replaced with hcl2. My json was working until I converted it to hcl2 and now I’m at a loss. Im looking at examples and my own files and I could use some help here.

The first one is basically just copy/paste from Vmware examples.

vsphere_endpoint            = "vcenter.internal.domain.net"
vsphere_username            = "Administrator@vsphere.local"
vsphere_password            = "blablabla"
vsphere_insecure_connection = true
vsphere_datacenter                     = "lab"
vsphere_cluster                        = "esxi.internal.domain.net"
vsphere_datastore                      = "ds_lab"
vsphere_network                        = "vmnet_lab"
vsphere_folder                         = "lab-packer-test"
vsphere_set_host_for_datastore_uploads = false

Then there is this packer-test:

packer {
  required_plugins {
    vsphere = {
      source  = "github.com/hashicorp/vsphere"
      version = "~> 1"
    }
  }
}
variable "HTTP_IP" {
  type    = string
  default = "10.0.0.10:81"
}
variable "HTTP_PATH" {
  type    = string
  default = "packer-test/ks.cfg"
}
variable "version" {
  type    = string
  default = "0.0.3"
}
source "vsphere-iso" "autogenerated_1" {
  CPU_hot_plug         = true
  CPUs                 = 2
  RAM                  = 4096
  RAM_hot_plug         = true
  boot_command         = ["<up><tab><bs><bs><bs><bs><bs><bs> text ip=dhcp hostname=packer-test inst.ks=http://${var.HTTP_IP}/${var.HTTP_PATH}<enter><wait><enter>"]
  cluster              = "${var.vsphere_cluster}"
  convert_to_template  = false
  create_snapshot      = false
  datacenter           = "${var.vsphere_datacenter}"
  datastore            = "${var.vsphere_datastore}"
  disk_controller_type = "pvscsi"
  folder               = "${var.lan-test}"
  guest_os_type        = "rockylinux_64Guest"
  host                 = "${var.vsphere_host}"
  insecure_connection  = "${var.vsphere_insecure_connection}"
  iso_paths            = [" [${var.vsphere_datastore}] isos/rocky-8.8-x86_64-dvd1.iso"]
  network_adapters {
    network      = "${var.vsphere_network}"
    network_card = "vmxnet3"
  }
  notes            = "Packer test RockyLinux version ${var.version}"
  password         = "${var.vsphere_password}"
  shutdown_command = "/sbin/halt -h -p"
  ssh_username     = "ingemar"
  ssh_private_key_file = "/Users/itsme/.ssh/id_rsa"
  storage {
    disk_size             = 120000
    disk_thin_provisioned = true
  }
  username       = "${var.vsphere_username}"
  vcenter_server = "${var.vsphere_host}"
  vm_name        = "packer-test"
}
build {
  sources = ["source.vsphere-iso.autogenerated_1"]
  provisioner "shell" {
    execute_command   = "bash '{{ .Path }}'"
    expect_disconnect = true
    script            = "${path.root}/requirements.sh"
  }
}

Whether I specify the build with -var-file= or without doesn’t seem to matter. From what I gather it looks for files in the same dir.

This what happens:
packer build packer-test.json.pkr.hcl

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 62:
  (source code not available)

This object does not have an attribute named "vsphere_insecure_connection".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 78:
  (source code not available)

This object does not have an attribute named "vsphere_host".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 53:
  (source code not available)

This object does not have an attribute named "vsphere_cluster".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 65:
  (source code not available)

This object does not have an attribute named "vsphere_network".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 56:
  (source code not available)

This object does not have an attribute named "vsphere_datacenter".

Error: Incorrect attribute value type

  on packer-test.json.pkr.hcl line 58:
  (source code not available)

Inappropriate value for attribute "disk_controller_type": list of string
required.

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 59:
  (source code not available)

This object does not have an attribute named "lan-test".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 57:
  (source code not available)

This object does not have an attribute named "vsphere_datastore".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 69:
  (source code not available)

This object does not have an attribute named "vsphere_password".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 63:
  (source code not available)

This object does not have an attribute named "vsphere_datastore".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 61:
  (source code not available)

This object does not have an attribute named "vsphere_host".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 77:
  (source code not available)

This object does not have an attribute named "vsphere_username".

So it doesn’t pick up any variable it seems?

When I specify both files with -var-file= the same thing happens:

packer build -var-file=vsphere.pkrvars.hcl packer-test.json.pkr.hcl

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 77:
  (source code not available)

This object does not have an attribute named "vsphere_username".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 56:
  (source code not available)

This object does not have an attribute named "vsphere_datacenter".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 63:
  (source code not available)

This object does not have an attribute named "vsphere_datastore".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 61:
  (source code not available)

This object does not have an attribute named "vsphere_host".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 69:
  (source code not available)

This object does not have an attribute named "vsphere_password".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 57:
  (source code not available)

This object does not have an attribute named "vsphere_datastore".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 78:
  (source code not available)

This object does not have an attribute named "vsphere_host".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 53:
  (source code not available)

This object does not have an attribute named "vsphere_cluster".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 59:
  (source code not available)

This object does not have an attribute named "lan-test".

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 62:
  (source code not available)

This object does not have an attribute named "vsphere_insecure_connection".

Error: Incorrect attribute value type

  on packer-test.json.pkr.hcl line 58:
  (source code not available)

Inappropriate value for attribute "disk_controller_type": list of string
required.

Error: Unsupported attribute

  on packer-test.json.pkr.hcl line 65:
  (source code not available)

This object does not have an attribute named "vsphere_network".

Could someone please have a look at those files and help me out?

Appreciate the help!
Thanks.

Hi @Pr71Dph,

From the error messages and the template, the variables you’re referencing are not defined.

If you have a variable file, you’ll need to include it with --var-file, as you did in your second invocation, but while the variable file sets values to the variables, you still need to define them in your template.

For your case, I see a couple: HTTP_PATH, version and HTTP_IP are examples of variables you defined with a default value, so if you don’t have another value for them, they will be using these.

For the remainder of the variables you’re invoking then, you’ll need to add blocks like these to your template so they’re declared, without a default value, but with a type.

For example for the vsphere_endpoint variable, that would be something like this:

variable "vsphere_endpoint" {
  type = string
}

Adding these blocks to the template should fix your problem.

Alternatively, for simple variable declarations with a default value, you can use a variables block, as shown in our docs. Fair warning though: the type is inferred by the type system in this case.

That being said, I’ll point out that the messages aren’t super clear, so I can completely understand why that’s confusing. I’ll see if something can be done so Packer provides better information on the root cause of the problem.

In the meantime if you add those blocks to your template, this should be enough to fix the problem you’re encountering.

Hope that helps!

1 Like

Hey there!

Thanks so much for your help, really appreciate it.

It got me a lot further but there is still something I don’t understand after reading the relevant docs etc, I don’t quite understand what I am doing wrong.

When I start the build, it says it cannot complete due to an authentication failure.

$ packer build -debug -var-file=vsphere.pkrvars.hcl packer-test.json.pkr.hcl
Debug mode enabled. Builds will not be parallelized.
vsphere-iso.autogenerated_1: output will be in this color.

    vsphere-iso.autogenerated_1: Closing sessions ....
Build 'vsphere-iso.autogenerated_1' errored after 5 seconds 31 milliseconds: ServerFaultCode: Cannot complete login due to an incorrect user name or password.

Obviously the first thing I checked is whether the username and password are specified correctly. It shows like this (pw obscured):

vsphere_username            = "Administrator@vsphere.local"
vsphere_password            = "LetM3iN@once"

The json worked with all those values, so I think something is off with the way I declared my variables.

In the packer-test.pkr.hcl I declared them below the HTTP vars you used by example:

variable "vsphere_endpoint" {
  type = string
}

variable "vsphere_username" {
  type = string
}

variable "vsphere_password" {
  sensitive = true
  type = string
}

variable "vsphere_insecure_connection" {
  type = bool
}

variable "vsphere_datacenter" {
  type = string
}

variable "vsphere_cluster" {
  type = string
}

variable "vsphere_datastore" {
  type = string
}

variable "vsphere_network" {
  type = string
}

variable "vsphere_folder" {
  type = string
}

variable "vsphere_set_host_for_datastore_uploads" {
  type = bool
}

I would simply define most of these as fixed values but the thing is, if / when I get this working we might be using this at work where we have 9 different vsphere clusters for different domains. I need to generate these files with jinja2 templates and Ansible.

Thanks again for your time! I hope you can help me out here, almost there :smile: .

To add to the above, adding defaults to the variable definition entries doesn’t help. When I change the vsphere_endpoint hostname to include https:// that doesn’t change the message. When I change the hostname to something that doesn’t exist or is even wrong, I get the same error message. Using an IP address doesn’t help.

It seems Packer does not even check whether the host specified is reachable and responds to API requests. It just throws that authentication failure even when it can’t authenticate at all.

It works with ‘root’. But now it can’t find the datacenter.

I don’t understand why packer behaves so differently with hcl2 files. Json worked fine with the exact same details.

Perhaps root doesn’t have permissions to access API. Im too annoyed right now to figure it out. Its been a hell to get this to work and its not like I’m new to this kind of thing. I do A LOT with Ansible and Puppet. I worked with Vagrant and getting the jsons to work with packer wasn’t so bad. Documentation isn’t helping either.

Does no one have an idea? :frowning:

I recreated my json files. They work like a charm.

I read this:

Checked my user has the correct permissions - it does.

My ansible scripts can use the same user credentials and talk to vSphere.

The above examples in hcl2 do not work. In vcenter event console I can see:

Cannot login packer@vsphere.local@10.0.0.2 Error 12/05/2023, 8:45:43 PM [ esxi.internal.domain.net ](javascript:void(0)) packer@vsphere.local vim.event.BadUsernameSessionEvent

Beginning of Expandable row content Screen reader table commands may not work for viewing expanded content, please use your screen reader's browse mode to read the content exposed by this button

Date Time: 12/05/2023, 8:45:43 PM
Type: Error
User: packer@vsphere.local
Target: [ esxi.internal.privatebits.net ](javascript:void(0))
Description: Cannot login packer@vsphere.local@10.0.0.2
Event Type Description: A user attempted to log in with an unknown or invalid username

Possible Causes:
* The username is unknown to the system Action: If you are using Active Directory, check the health of the domain controller
* The user provided an invalid password Action: Supply the correct password

I made the password simpler, different special characters like replacing a @ for a *.

I can’t get hcl files to work.

When I was unable to make it work I deleted it and continued using json. I gave conversion another try, corrected the files with the above info and got hcl working with basically the same variables and values. Pity I don’t have the previous hcl files anymore to what is different.