Creating a template in vSphere allows for rapid deployment of VMs. You can add or update custom software and build the perfect server to be consitently deployed in your enviornment.
My goal is to create a VM, add VMware tools, and strip out any unique data.
Before we start
- Download the ISO of Ubuntu 18.04 LTS
- Upload the Ubuntu 18.04 ISO to a vSphere datastore.
- Create a VM using that ISO (including full post install / OS setup)
- SSH into the newly created VM
Customize the template
I’ve included the manual steps below that are needed to clean up your template. If you want to take the fast track you can just run this script and skip to the next section.
Update all packages
# use caution when using -y (automatic "yes") sudo apt -y update sudo apt -y upgrade
Install VMware tools
# most likely is already installed sudo apt -y install open-vm-tools
Strip out unique data
#Stop services for cleanup sudo service rsyslog stop #clear audit logs # -s0 sets the bites to 0 erasing the file sudo truncate -s0 /var/log/wtmp sudo truncate -s0 /var/log/lastlog #cleanup /tmp directories sudo rm -rf /tmp/* sudo rm -rf /var/tmp/* #cleanup current ssh keys sudo rm -f /etc/ssh/ssh_host_* #add check for ssh keys on reboot...regenerate if neccessary cat <<EOL | sudo tee /etc/rc.local #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # dynamically create hostname if hostname | grep localhost; then hostnamectl set-hostname $(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13 ; echo '') fi # By default this script does nothing. test -f /etc/ssh/ssh_host_dsa_key || dpkg-reconfigure openssh-server exit 0 EOL # make the script executable sudo chmod +x /etc/rc.local #reset hostname # prevent cloudconfig from preserving the original hostname sudo sed -i 's/preserve_hostname: false/preserve_hostname: true/g' /etc/cloud/cloud.cfg sudo truncate -s0 /etc/hostname sudo hostnamectl set-hostname localhost #cleanup apt sudo apt clean # cleans out all of the cloud-init cache / logs - this is mainly cleaning out networking info sudo cloud-init clean --logs #cleanup shell history history -c history -w
Optional configuration for kubeadm
# disable swap sudo swapoff --all sudo sed -ri '/\sswap\s/s/^#?/#/' /etc/fstab # TO DO: bring the hostname junk down here
Add as a template to vSphere
At this point, we’ve customized the VM and it should be shut off.
Make sure to disconnect the CDROM and the NIC before adding as template
Right Click VM > Edit Settings > deselect…
- Right Click VM > convert to template
That’s it! I plan on using these VMs for my needs