Automating your VMware Homelab with Vagrant
Introduction
One of the best ways to learn and hone IT skills where you would otherwise need to have hands on experience, is by testing out or implementing cool technologies like these in a homelab.
Remember that old Catch-22?
- How can I get the job without any experience?
- How can I get any experience without the job?
A Homelab helps with that.
Requirements
You will need to have at least one of the following Virtual Machine providers:
Virtualbox, Hyper-V, ESXI, or VMWARE.
For the purposes of this lab deployment I will be utilizing the Vagrant plugin for the VMWare provider, however the same or similar steps can still be followed if you use different providers. If you would like to do the same, but for ESXI I recommend you check out this awesome walk through by Darthsidious on building a lab.
If you're interested in having a Virtual Network Customization feature, creating VM clones, or creating snapshots among other awesome features then a VMWare or ESXI provider should be the way to go.
Don't have the money for VMWare Workstation Pro?
You can try evaluation version for 30 days for VMWare Workstation Pro.
Final Lab Map - Sneak Preview
I made this diagram with with the Draw.io tool for Desktop
Setup Steps
To get started we will need to prep an ISO to be ready for a deployment with Vagrant.
Packer helps us automate some of that tiresome process of preparing images into VMs ready for Vagrant installation.
- Download Chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))```
- Install Git via Chocolatey (if you don't already have it)
cinst git
- Install Vagrant via Chocolatey
cinst vagrant
- Install packer via Chocolatey
cinst packer -y
- Create a directory for packer boxes
cmd /k "mkdir C:\Packer && cd C:\Packer"
The VMware Packer builder by Hashicorp
The VMware Packer builder is able to create VMware virtual machines for use with any VMware product.
Packer supports the following VMware builders:
vmware-iso - Starts from an ISO file, creates a brand new VMware VM, installs an OS, provisions software within the OS, then exports that machine to create an image. This is best for people who want to start from scratch.
vmware-vmx - This builder imports an existing VMware machine (from a VMX file), runs provisioners on top of that VM, and exports that machine to create an image. This is best if you have an existing VMware VM you want to use as the source. As an additional benefit, you can feed the artifact of this builder back into Packer to iterate on a machine.
Preparing our VMDKs for Packer Builder
To prepare the VMDKs we will...
You can download a copy of the vmware-vdiskmanager for windows or linux to do this by command line. I also provide the required files (and the missing DLLs) on my Google Drive.
- Open the settings for the VM
- Select Hard Disk
- Click Defragment
- Click Compact
- Compress the VMDK directory with the command below
The files that are strictly required for a VMware machine to function are: nvram, vmsd, vmx, vmxf, and vmdk files.
More details on preparing VMDKs for .BOX format can be found below...
Create a JSON in the packer directory for your VM
Convert the JSON to a .PKR.HCL with the following commands and build it
packer hcl2_upgrade build_flarevm.pkr.json
packer build build_flarevm.pkr.json.pkr.hcl