In my previous post I explained the tools I use in my lab automation. Today, we’ll talk about creating Ubuntu templates into VMware vSphere. For this, I will use Packer.
Prepare the system
iso_path = "[vsanDatastore] contentlib-5ed427e9-8cf4-442a-8218-14a8d3cc9dc3/ab0bf964-474e-4e4d-90dd-a324b392d660/ubuntu-22.04.2-live-server-amd64_1641f4b9-dbe1-409a-9433-7cd39c9dbfe7.iso"
I do this because I didn’t find a way to pass the library information to Packer, while I can mount an ISO from a datastore.
Passwords and other data
user-data
#cloud-config autoinstall: version: 1 early-commands: # workaround to stop ssh for packer as it thinks it timed out - sudo systemctl stop ssh locale: en_US keyboard: layout: en variant: us packages: [open-vm-tools, openssh-server, net-tools, network-manager, perl, open-iscsi, ntp, curl, vim, ifupdown, zip, unzip, gnupg2, software-properties-common, apt-transport-https, ca-certificates, lsb-release, python3-pip, jq, cloud-init] network: network: version: 2 ethernets: ens192: dhcp4: true identity: hostname: ubuntu2204 username: ubuntu password: "$6$rounds=4096$pwAk.wxvgujkbm$4x724AhMsslbqtp4dISZppC8sHsNmafcVl.R0B0qZ2q8i2TH4/x4uWfpz1JOX3glSMPo4leBoxn7Oyr62U3In." ssh: install-server: yes allow-pw: yes authorized-keys: - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCb7fcDZfIG+SxuP5UsZaoHPdh9MNxtEL5xRI71hzMS5h4SsZiPGEP4shLcF9YxSncdOJpyOJ6OgumNSFWj2pCd/kqg9wQzk/E1o+FRMbWX5gX8xMzPig8mmKkW5szhnP+yYYYuGUqvTAKX4ua1mQwL6PipWKYJ1huJhgpGHrvSQ6kuywJ23hw4klcaiZKXVYtvTi8pqZHhE5Kx1237a/6GRwnbGLEp0UR2Q/KPf6yRgZIrCdD+AtOznSBsBhf5vqcfnnwEIC/DOnqcOTahBVtFhOKuPSv3bUikAD4Vw7SIRteMltUVkd/O341fx+diKOBY7a8M6pn81HEZEmGsr7rT ubuntu@ubuntu.local storage: layout: name: direct user-data: disable_root: false late-commands: - echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/ubuntu - curtin in-target --target=/target -- chmod 440 /etc/sudoers.d/ubuntu
sudo apt-get install whois mkpasswd -m sha-512 --rounds=4096
ssh-keygen
or, if I already have it, I can read it using:
cat ~/.ssh/id_rsa.pub
ubuntu2204.pkr.hcl
cd_files = [ "./${var.http_directory}/meta-data", "./${var.http_directory}/user-data"]
Also, in the build section I invoke a specific builder for VMware vSphere:
build { sources = [ "source.vsphere-iso.linux-ubuntu-server"]
vsphere.pkrvars.hcl
################################################################################## # VARIABLES ################################################################################## # Credentials vcenter_username = "administrator@vsphere.local" vcenter_password = "password" # vSphere Objects vcenter_insecure_connection = true vcenter_server = "vcenter.vsphere.local" vcenter_datacenter = "Datacenter" vcenter_host = "esx.vsphere.local" vcenter_datastore = "vsanDatastore" vcenter_network = "VM Network" vcenter_folder = "Templates" # ISO Objects iso_path = "[vsanDatastore] contentlib-5ed427e9-8cf4-442a-8218-14a8d3cc9dc3/ab0bf964-474e-4e4d-90dd-a324b392d660/ubuntu-22.04.2-live-server-amd64_1641f4b9-dbe1-409a-9433-7cd39c9dbfe7.iso"
Obviously, you will need to adjust these values to be compatible with your own environment.
ubuntu2204.pkrvars.hcl
################################################################################## # VARIABLES ################################################################################## # HTTP Settings http_directory = "http" # Virtual Machine Settings vm_name = "ubuntu2204" vm_guest_os_type = "ubuntu64Guest" vm_version = 17 vm_firmware = "bios" vm_cdrom_type = "sata" vm_cpu_sockets = 1 vm_cpu_cores = 2 vm_mem_size = 4096 vm_disk_size = 20480 thin_provision = true disk_eagerly_scrub = false vm_disk_controller_type = ["pvscsi"] vm_network_card = "vmxnet3" vm_boot_wait = "5s" ssh_username = "ubuntu" ssh_password = "ubuntu" # ISO Objects iso_file = "ubuntu-22.04.1-live-server-amd64.iso" iso_checksum = "10f19c5b2b8d6db711582e0e27f5116296c34fe4b313ba45f9b201a5007056cb" iso_checksum_type = "sha256" iso_url = "https://releases.ubuntu.com/jammy/ubuntu-22.04.1-live-server-amd64.iso" # Scripts shell_scripts = ["./scripts/setup_ubuntu2204.sh"]
Here I customize the virtual machine I’m going to build.
Build the VM
packer init .
This will verify that the system is ready to execute my scripts; on the first run it will install all the needed additional packages.
packer build -force -on-error=ask -var-file ubuntu2204.pkrvars.hcl -var-file vsphere.pkrvars.hcl ubuntu2204.pkr.hcl
Let’s go grab a coffee, and let Packer work on this: