packer-plugin-vmware icon indicating copy to clipboard operation
packer-plugin-vmware copied to clipboard

SSHPublicKey created too late for cd_content

Open puetzk opened this issue 1 year ago • 3 comments

Overview of the Issue

#96 added support for StepSSHKeyGen, but runs it quite late in the sequence (most notably, after StepCreateFloppy and StepCreateCD).

https://github.com/hashicorp/packer-plugin-vmware/blob/a40ad8f7c745655d35be19c07d785636b92c8625/builder/vmware/iso/builder.go#L126-L129

https://github.com/hashicorp/packer-plugin-vmware/blob/a40ad8f7c745655d35be19c07d785636b92c8625/builder/vmware/iso/builder.go#L77-L87

This means the SSHPublicKey template variable isn't avilable in time to use in something like cloud-init metadata, e.g. https://cloudinit.readthedocs.io/en/latest/reference/datasources/nocloud.html

  cd_content = {
    meta-data = "instance-id: \"ubuntu-${uuidv4()}\""
    user-data = <<-EOF
      #cloud-config
      manage_etc_hosts: localhost
      disable_root: false
      ssh_authorized_keys:
        - {{ .SSHPublicKey }}
      EOF
  }

Reproduction Steps

packer.exe build -debug ubuntu-jammy.pkr.hcl

After reaching

Pausing after run of step 'StepCreateCD' inspect %TEMP%\packer##########.iso and observe that it says

#cloud-config
manage_etc_hosts: localhost
disable_root: false
ssh_authorized_keys:
  - <no value>

I.e. {{ .SSHPublicKey }} substituted <no value> instead of having a public key to use (because it hasn't been generated yet).

Plugin and Packer version

From packer version

packer-plugin-vmware_v1.0.11_x5.0_windows_amd64.exe

Simplified Packer Buildfile

source "vmware-vmx" "vmware" {
  cd_content = {
    meta-data = "instance-id: \"ubuntu-${uuidv4()}\""
    user-data = <<-EOF
      #cloud-config
      manage_etc_hosts: localhost
      disable_root: false
      ssh_authorized_keys:
        - {{ .SSHPublicKey }}
      EOF
  }
  cd_label             = "cidata"
  communicator         = "ssh"
  display_name         = "ubuntu"
  headless             = "true"
  output_directory     = "ubuntu"
  shutdown_command     = "shutdown -P now"
  source_path          = "ubuntu-jammy-cloudimg/ubuntu-jammy-cloudimg.vmx"
  ssh_username         = "root"
  vm_name              = "ubuntu"
  vmx_data = {
    "bios.bootOrder"        = "hdd"
    "bios.hddOrder"         = "scsi0"
    "ide1:0.startConnected" = "TRUE"
    "rtc.diffFromUTC"       = "0"
  }
}

build {
  sources = ["source.vmware-vmx.vmware"]
}

source_path is imported from e.g. https://cloud-images.ubuntu.com/releases/jammy/release/ubuntu-22.04-server-cloudimg-amd64.ova

Operating system and Environment details

Windows 10 x64

Log Fragments and crash.log files

This ordering can also be clearly seen in the output of C:\inclement\packer.exe build -debug ubuntu-jammy.pkr.hcl

==> vmware-vmx.vmware: Pausing after run of step 'StepPrepareTools'. Press enter to continue.
==> vmware-vmx.vmware: Configuring output and export directories...
==> vmware-vmx.vmware: Pausing after run of step 'StepOutputDir'. Press enter to continue.
==> vmware-vmx.vmware: Pausing after run of step 'StepCreateFloppy'. Press enter to continue.
==> vmware-vmx.vmware: Creating CD disk...
    vmware-vmx.vmware: Warning: creating filesystem with (nonstandard) Joliet extensions
    vmware-vmx.vmware: but without (standard) Rock Ridge extensions.
    vmware-vmx.vmware: It is highly recommended to add Rock Ridge
    vmware-vmx.vmware: Total translation table size: 0
    vmware-vmx.vmware: Total rockridge attributes bytes: 0
    vmware-vmx.vmware: Total directory bytes: 156
    vmware-vmx.vmware: Path table size(bytes): 10
    vmware-vmx.vmware: 182 extents written (0 MB)
    vmware-vmx.vmware: Done copying paths from CD_dirs

==> vmware-vmx.vmware: Pausing after run of step 'StepCreateCD'. Press enter to continue. ==> vmware-vmx.vmware: Pausing after run of step 'StepRemoteUpload'. Press enter to continue.
==> vmware-vmx.vmware: Creating required virtual machine disks
==> vmware-vmx.vmware: Pausing after run of step 'StepCreateDisks'. Press enter to continue.
==> vmware-vmx.vmware: Cloning source VM...

==> vmware-vmx.vmware: Pausing after run of step 'StepCloneVMX'. Press enter to continue. ==> vmware-vmx.vmware: Pausing after run of step 'StepConfigureVMX'. Press enter to continue.
==> vmware-vmx.vmware: Pausing after run of step 'StepSuppressMessages'. Press enter to continue.
==> vmware-vmx.vmware: Pausing after run of step 'StepHTTPIPDiscover'. Press enter to continue.
==> vmware-vmx.vmware: Pausing after run of step 'StepHTTPServer'. Press enter to continue.
==> vmware-vmx.vmware: Creating temporary RSA SSH key for instance...
==> vmware-vmx.vmware: Pausing after run of step 'StepSSHKeyGen'. Press enter to continue.

puetzk avatar Apr 11 '24 17:04 puetzk

communicator.StepSSHKeyGen doesn't really need input, so I think it just needs to be called earlier...

puetzk avatar Apr 11 '24 17:04 puetzk

Hi @puetzk,

This looks indeed feasible, and this would be rather simple to fix at first glance. Would you be open to make that change and PR it to that plugin?

Please let me know, I'll gladly review and help if needed!

lbajolet-hashicorp avatar Apr 15 '24 13:04 lbajolet-hashicorp

Happy to take care of it if you're not comfortable with it @puetzk. Just let me know.

tenthirtyam avatar Apr 16 '24 01:04 tenthirtyam