Skip to content

IP address gets changed after reboot (not always) when building using vsphere - no route to host ssh #425

@LeifErik1995

Description

@LeifErik1995

Hi folks,

I am experiencing an issue with IP address where old one not getting recognized after the reboot when building ubuntu 22.04, host ESXi 7.0. After reboot IP is no longer assigned to the VM.

source "vsphere-iso" "ubuntu" {

  vcenter_server        = var.vsphere_server
  host                  = var.vsphere_host
  username              = var.vsphere_username
  password              = var.vsphere_password
  insecure_connection   = "true"
  datacenter            = var.vsphere_datacenter
  datastore             = var.vsphere_datastore

  CPUs                  = var.cpu_num
  RAM                   = var.mem_size
  RAM_reserve_all       = true
  disk_controller_type  = ["pvscsi"]
  guest_os_type         = "ubuntu64Guest"
  iso_checksum       = "45f873de9f8cb637345d6e66a583762730bbea30277ef7b32c9c3bd6700a32b2"

  iso_url = "[datastore1 (38)] packer_cache/67a67612e798509328c9ee5490f7388971ed878b.iso"
  cd_content            = {
    "/meta-data" = file("${var.cloudinit_metadata}")
    "/user-data" = file("${var.cloudinit_userdata}")
  }
  cd_label              = "cidata"

  network_adapters {
    network             = var.vsphere_network
    network_card        = "vmxnet3"
  }
  
  storage {
    disk_size = var.disk_size
    disk_thin_provisioned = true
  }
  storage {
    disk_size = var.disk_size
    disk_thin_provisioned = true
  }

  vm_name               = var.vsphere_vm_name
  convert_to_template   = "true"
  communicator          = "ssh"
  ssh_username          = var.ssh_username
  ssh_password          = var.ssh_password
  ssh_timeout           = "30m"
  ssh_handshake_attempts = "100000"
  export {
    force = true
    output_directory = "./output-artifacts"
  }

  boot_wait             = "3s"
  boot_command          = var.boot_command
  shutdown_command      = "echo '${var.ssh_password}' | sudo -S -E shutdown -P now"
  shutdown_timeout      = "15m"

  configuration_parameters = {
    "disk.EnableUUID" = "true"
  }
}

My cloud-init is as below

#cloud-config
autoinstall:
    version: 1
    early-commands:
        # Stop ssh for packer
        - sudo systemctl stop ssh
    locale: en_US
    keyboard:
        layout: us
        #variant: us
    identity:
        hostname: ubuntu-server
        username: ubuntu
        password: '$6$rounds=4096$.yfET/Pf2chKB5IS$vUcqTvUzup6eYSx.wajZD0zHTobzapEKKjw5GvXbRonLJqB08eRhB1W6Wkhq6Kp4WzC19F3uWNoocqlPgZLrv.'
    ssh:
        install-server: yes
        allow-pw: yes
    storage:
        layout:
            name: direct
    packages: [open-vm-tools, cloud-init, openssh-server, python3, sshpass, net-tools, perl, open-iscsi, ntp, curl, vim, ifupdown, zip, ufw, unzip, gnupg2, gnupg-agent, software-properties-common, apt-transport-https, ca-certificates, lsb-release, python3-pip, jq]
    user-data:
        disable_root: false
    ssh_deletekeys: false
    late-commands:
        # Prevent DHCP release message from being sent on reboot
        - iptables -I OUTPUT -p udp --dport 67 -j DROP
        - sed -i -e 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /target/etc/ssh/sshd_config
        - sed -i -e 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /target/etc/ssh/sshd_config
        - echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/ubuntu
        - curtin in-target --target=/target -- ssh-keygen -A
        - curtin in-target --target=/target -- systemctl enable ssh
        - curtin in-target --target=/target -- systemctl start ssh
        - curtin in-target --target=/target -- chmod 440 /etc/sudoers.d/ubuntu
        - curtin in-target --target=/target -- apt-get update
        - curtin in-target --target=/target -- apt-get upgrade --yes

Boot command is as follows

boot_command = [
    "c<wait>",
    "linux /casper/vmlinuz --- autoinstall ds=\"nocloud-net\"",
    "<enter><wait>",
    "initrd /casper/initrd",
    "<enter><wait>",
    "boot",
    "<enter>",
    "<wait30>"
    ]

I scoured through the issues that relate to this to make some headway on this, but there seems to be some issue with my config.
<BTW, I am very new to packer, so any advice is appreciated however trivial it might be>.
Also attaching the screenshot of both the after reboot vm log, and the packer debug log at the time of the error. After that and i get the no route to host error, the vm ends up in the state of the final image.

Thanks

Screenshot 2024-05-04 at 12 14 12 AM
Screenshot 2024-05-04 at 12 13 56 AM
Screenshot 2024-05-04 at 12 26 57 AM
Screenshot 2024-05-04 at 12 32 46 AM

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions