Automating your VMware Homelab with Vagrant

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.

  1. 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'))```
    
  2. Install Git via Chocolatey (if you don't already have it)
    cinst git
  3. Install Vagrant via Chocolatey
    cinst vagrant
  4. Install packer via Chocolatey
    cinst packer -y
  5. Create a directory for packer boxes
    cmd /k "mkdir C:\Packer && cd C:\Packer"
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'))

cinst git -y

cinst vagrant -y

cinst packer -y

cmd /k "mkdir C:\Packer && cd C:\Packer"
Installation Steps

Packer by HashiCorp
The VMware Packer builder is able to create VMware virtual machines for usewith any VMware product.

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.

packer plugins install github.com/hashicorp/vmware
Add the plugin required by packer for VMware

Preparing our VMDKs for Packer Builder

To prepare the VMDKs we will...

Disk Utilities from VM Settings 

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.

  1. Open the settings for the VM
  2. Select Hard Disk
  3. Click Defragment
  4. Click Compact
  5. Compress the VMDK directory with the command below
cd /path/to/my/vm.directory/
tar cvzf custom.box ./*
and finally - Compressing the VM directory
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...

Vagrant by HashiCorp
As with every Vagrant provider, the Vagrant VMware providers have a custom boxformat.

Create a JSON in the packer directory for your VM

{
  "builders": [
    {
	  "type": "vmware-vmx",
	  "source_path": "C:/Users/Administrator/Documents/vm_storage/FlareVM/FlareVM.vmx",
	  "ssh_username": "user",
	  "ssh_password": "password",
	  "shutdown_command": "shutdown -s -t 0"
	}
  ]
}
build-flarevm.pkr.json

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
Show Comments