7

My Automated Lab project: #2 Create a Ubuntu template in VMware vSphere with Pac...

 9 months ago
source link: https://www.virtualtothecore.com/my-automated-lab-project-2-create-a-ubuntu-template-in-vmware-vsphere-with-packer/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

My Automated Lab project: #2 Create a Ubuntu template in VMware vSphere with Packer

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.

NOTE: You can grab all the files that I’m presenting here from my Github repo:
Some parameters in the public files have been anonymized, please customize the values to suit your needs.

Prepare the system

Before being able to run Packer, I need to create its scripts, but even before I need the data that I will put into the scripts.
Let’s start with the ISO file from where I will install Ubuntu 22.04. I grab the needed Ubuntu ISO and I save it in a vSphere library. I usually get the “live server” version, as it has all the packages available in the ISO. I prefer this method compared to the direct download of live ISO each time I run the script (a common solution that i’ve seen in many similar articles) because it’s faster to run after I only grabbed the ISO once, and it can work also when there is some connectivity issue.
This is the content of my library:
but what I need is its physical path in the underlying datastore, that I can read by browsing the datastore itself:
Pasted.png?resize=640%2C159&ssl=1
This information will be passed as a variable in the Packer script, like this:
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"
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

This is the structure of the files used by Packer:
I need to edit some of them to enter the data I need to customize the VM I will create. Let’s start from top to bottom.

user-data

The user-data file is in the http folder along with an empty file called meta-data. This meta-data file is required for cloud deployments, but since I’m not deploying to the cloud I can leave it empty. But I need to customize the user-data file in some points:
#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."
install-server: yes
allow-pw: yes
authorized-keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCb7fcDZfIG+SxuP5UsZaoHPdh9MNxtEL5xRI71hzMS5h4SsZiPGEP4shLcF9YxSncdOJpyOJ6OgumNSFWj2pCd/kqg9wQzk/E1o+FRMbWX5gX8xMzPig8mmKkW5szhnP+yYYYuGUqvTAKX4ua1mQwL6PipWKYJ1huJhgpGHrvSQ6kuywJ23hw4klcaiZKXVYtvTi8pqZHhE5Kx1237a/6GRwnbGLEp0UR2Q/KPf6yRgZIrCdD+AtOznSBsBhf5vqcfnnwEIC/DOnqcOTahBVtFhOKuPSv3bUikAD4Vw7SIRteMltUVkd/O341fx+diKOBY7a8M6pn81HEZEmGsr7rT [email protected]
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
#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 [email protected]
    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
As you can read, I configure here the keyboard layout first; then I add the list of the packages I want to automatically install: open-vm-tools are obviously paramount to have the VM be able to use the virtual hardware; openssh-server will be used to connect to the VM from remote and configure it, plus other programs for the networking part and other needs.
For the network, I will use DHCP: there is a way to configure a VM with a static IP, but this is a template so I don’t want to stick with a fixed IP address. Obviously, to create this VM I will need a DHCP server listening on the same portgroup where I will connect this VM.
In the identity section I configure the hostname and I create a default ubuntu user. The password is not written in clear text, but it’s hashed using the following procedure.
I install whois (since mkpasswd is part of this package) and then I input the password so that mkpasswd will return its hash:
sudo apt-get install whois
mkpasswd -m sha-512 --rounds=4096
sudo apt-get install whois 
mkpasswd -m sha-512 --rounds=4096
In the authorized-keys section, I can insert my own SSH key that I have in my management Linux machine, the one I’ll use to connect later to the VM. I can create my own SSH key by using:
ssh-keygen
ssh-keygen

or, if I already have it, I can read it using:

cat ~/.ssh/id_rsa.pub
cat ~/.ssh/id_rsa.pub

ubuntu2204.pkr.hcl

This is the main packer file that I will use it to build my machine. There is nothing to configure here. It’s however interesting to notice that user-data is mounted as a cd-rom to be used in the configuration:
cd_files = [
"./${var.http_directory}/meta-data",
"./${var.http_directory}/user-data"]
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"]
build {
  sources = [
    "source.vsphere-iso.linux-ubuntu-server"]

vsphere.pkrvars.hcl

This is the file where I store all the vSphere information:
##################################################################################
# VARIABLES
##################################################################################
# Credentials
vcenter_username = "[email protected]"
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"
##################################################################################
# VARIABLES
##################################################################################
# Credentials
vcenter_username                = "[email protected]"
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

This is the file where I store all the VM information:
##################################################################################
# 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"]
##################################################################################
# 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

I start by initializing the Packer environment:
packer init .
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.

Once it’s ready, I start the building script:
packer build -force -on-error=ask -var-file ubuntu2204.pkrvars.hcl -var-file vsphere.pkrvars.hcl ubuntu2204.pkr.hcl
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:

Pasted-1.png?resize=640%2C271&ssl=1
After a few minutes, if all worked correctly, it should end up with a screen like this:
I have built my new Ubuntu 22.04 template. It took 17 minutes, but I only spent a few seconds running the command, and I’ve used the rest of the time to do something else. And every time I need to update the template, I will just destroy it and build a new one.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK