Packer tries to connect over ssh and gives up

Hello,

I’m using packer to connect to my vsphere cluster. This is my current config:

source "vsphere-iso" "comp-local" {
# connection configuration
  username       = "${var.vsphere_username}"
  vcenter_server = "${var.vsphere_host}"
  password         = "${var.vsphere_password}"

  ssh_password     = "${var.ssh_password}"
  ssh_timeout      = "30m"
  ssh_username     = "${var.ssh_username}"

# location configuration
  cluster              = "${var.vsphere_cluster}"
  host                 = "${var.vsphere_build_host}"
  datastore            = "${var.vsphere_datastore}"
  folder               = "${var.vsphere_folder}"

# vm configuration
  CPUs                 = 4
  RAM                  = 6144
  guest_os_type        = "ubuntu64Guest"
  storage {
    disk_size             = 10000
    disk_thin_provisioned = true
  }
  vm_name        = "${var.template_name}"
  remove_cdrom		= true
  network_adapters {
    network      = "${var.vsphere_network}"
    network_card = "vmxnet3"
  }
  disk_controller_type = [ "pvscsi" ]
  convert_to_template  = true

# boot configuration
  boot_command	= [
		"<e><bs><down><down><down>",
		"<right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right><right>",
		"<spacebar>",
		"ip=${var.vm_ip}::${var.vm_gateway}:${var.vm_netmask}::::${var.vm_dns} ",
		"autoinstall 'ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/' ",
		"<F10>"
		]
  boot_wait	= "4s"
  http_directory	= "http"
  http_port_max	= 8039
  http_port_min	= 8039
  insecure_connection	= false
  iso_checksum	= "sha256:10f19c5b2b8d6db711582e0e27f5116296c34fe4b313ba45f9b201a5007056cb"
  iso_paths		= [
			"[vmstorage] iso/ubuntu-22.04.1-live-server-amd64.iso"
			]
  shutdown_command = "sudo -S shutdown -P now"
}

# a build block invokes sources and runs provisioning steps on them. The
# documentation for build blocks can be found here:
# https://www.packer.io/docs/templates/hcl_templates/blocks/build
build {
  sources = ["source.proxmox.comp-local", "source.vsphere-iso.comp-local"]

  provisioner "ansible" {
    extra_arguments = ["--extra-vars", "vmware_build=${var.vmware_build}"]
    playbook_file   = "${var.playbook}"
    user            = "packer"
  }

}

The issue that I’m having, which strangely enough doesn’t occur with proxmox, is that packer tries to connect to the VM over ssh while the installer is still running, so before the reboot. The ssh service starts running, and this somehow throws packer. The error that I’m getting is this:

Packer experienced an authentication error when trying to connect via SSH. This can happen if your username/password are wrong. You may want to double-check your credentials as part of your debugging process. original error: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none password], no supported methods remain

I don’t remember exactly how this works, but I’m guessing packer tries to connect at certain intervals until it’s successful, so then it probably doesn’t know when the virtual machine will reboot (?). In this case, though, packer sees that the ssh service is running and takes the failed authentication as a valid answer and gives up after around 3 minutes, which I’m guessing it’s about the time it actually first tries to connect.
The ssh credentials work only after the virtual machine restarts, so after its dedicated user is created. I wouldn’t want it to succesfully connect to the VM during installation anyway, for obvious reasons!

Any ideas why this might happen and where the difference from proxmox might be?

Thanks!

Inspired by this thread Packer connecting to Ubuntu "too early", how to enforce a delay? I was able to make it work by stopping the ssh service altogether through early-commands in autoinstall:

early-commands:
  systemctl stop ssh