Vagrant is a useful and free software product that automates the provisioning of new virtual machines. Its primary purpose is to bring up VMs on your local machine quickly, so you don't have to worry about setting up the infrastructure just to test something. Its goal, which it attains, is to allow you to type "vagrant up" and immediately have one or more VMs automatically come up and be available.

As IT professionals, we wear lots of hats. One of those hats is typically the "new project" hat. I'm talking about the one that's inherited when your boss comes in and says, "We need to look into application X. Download it, play around with it, and give the team a demo of it in three weeks." This is the start of the infamous proof of concept (POC). This is when you have no experience with a product and need to bring it up quickly and poke at it.

What's your first step? Do you create a VM on your company's VMware or Hyper-V environment, wait a week for other teams to provision resources for you, and then hear constant reminders not to fool with "production stuff," because this is just a test thing? If so, there's a better way—with a product called Vagrant.

Vagrant isn’t incredibly complicated and doesn’t break any new ground. After all, it's just automating what we could do ourselves by installing Hyper-V, VirtualBox, or some other virtualization software on our local machine and setting up the VMs. What sets it apart is the huge timesaving effort it affords you. Once a single text file is set up, with just a single command, VMs magically appear on your machine, ready to be connected. In this article, I'll give you a peek behind the scenes of this magic, so you can enjoy this benefit as well.

To start, you'll need to install a couple things. First, you'll need to download Vagrant itself, and second, you'll need a "provider" for Vagrant—the hypervisor that will bring up your VMs. Vagrant supports VirtualBox, Hyper-V, and Docker out of the box but also can plug into others. For this demonstration, I'll be using VirtualBox on a Windows machine.

Once you have both products installed, fire up PowerShell and create a directory, if Vagrant didn't create one for you. You'll learn that Vagrant is picky about file locations. I'll create one called C:\Vagrant and change into it.

cd C:\Vagrant

At this point, I need to tell Vagrant a few basic things about what kind of VMs I'd like it to provision for me. In this case, I'm going to bring up a Windows Server 2012 R2 VM. Vagrant calls these VMs "boxes." You can download boxes automatically from the public box catalog. You'll find that hundreds of different images exist already set up for use with Vagrant.

If provisioning Windows Server boxes, I recommend looking for boxes created by Matt Wrock (mwrock). He takes a lot of time to streamline his images, making them as lightweight as possible. For this demonstration, I'll use his Windows2012R2 box.

Now that I know what box I want to use, I'll create a configuration file where I can specify the box name I want and a few other details. To do this, I'll create a Ruby script called VagrantFile (no extension) in my C:\Vagrant directory.

Add-Content –Path C:\Vagrant\VagrantFile –Value ''

At a minimum, for a Windows box, your VagrantFile should include:

  • Box name
  • Hostname
  • Communicator reference (winrm)
  • Username
  • Password
  • Private IP address
  • Memory
  • CPU

These attributes are defined in the file like this:

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
	config.vm.define "S1" do |web01|
		web01.vm.box = "mwrock/Windows2012R2"
		web01.vm.hostname = "S1"
		web01.vm.communicator = "winrm"
		web01.winrm.username = "vagrant"
		web01.winrm.password = "vagrant"
		web01.vm.network "private_network",ip: "192.168.2.6"
		web01.vm.provider "virtualbox" do |vb|
			 vb.memory = 2048
			 vb.cpus = 1
		 end
	end
end

There are many different ways to define this, but you'll find that if you need to provision multiple VMs, this is the easiest way.

Now that I've got my configuration file set up, I'll simply run "vagrant up" and it will begin to download the box and set it up for me. You can see below that it's importing the base box. This is because I've downloaded this box before and it caches it. If this is a new install, it will download it from the public box catalog. You'll also see that it literally automates all the common things that have to happen to get a functioning VM working, installing the VirtualBox additions, setting up all the networking, configuring WinRM, setting the hostname, and more.

Automates all the common things that have to happen to get a functioning VM

Automates all the common things that have to happen to get a functioning VM

Now that it’s up, Vagrant knows how to connect to it. I can then use PowerShell remoting to connect using the vagrant/vagrant credential, although I could simply RDP into the server if I wish.

icm –computername 192.168.2.6 –ScriptBlock {hostname} –Credential (Get-Credential)
Use PowerShell remoting to connect using the vagrantvagrant credential

Use PowerShell remoting to connect using the vagrantvagrant credential

Once I'm done with the box, I can destroy it just as easily with "vagrant destroy."

Vagrant destroy

Vagrant destroy

This was Vagrant 101. If you dive deeper, you'll see that Vagrant can automate many more aspects of VM provisioning through multi-VM test labs, spinning up VMs on Azure, AWS, and more. If you routinely need to bring up VMs quickly to develop on or just want to poke around with a new product, I encourage you to check out Vagrant.

Subscribe to 4sysops newsletter!

In my next post, I will show you set up a Vagrant test environment.

0 Comments

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2022

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account