- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
Vagrant is a valuable tool if you need a few VMs to test some kind of server application. It’s also a free tool and acts like an automation layer between the user and a virtual environment. Gone are the days of having to install Hyper-V, VmWare ESXi, or some other hypervisor on a server somewhere, create VMs, create the VHDs, set up networking, etc., just to do some simple testing. This is where Vagrant saves you tons of time.
This article will be about taking your skills up a notch if you'd like to set up an entire environment. You'll learn how to test a high-availability situation, such as setting up load balancers, clusters, and the like. I've also personally used this to test Ansible managing Windows, which required a Linux and a Windows host.
Let's get into the logistics of how to use Vagrant to set up three VMs on your local machine. Vagrant is not limited to three VMs, but my machine is! For this demonstration, I'm going to create an Ubuntu Linux server, a Windows Server 2012 R2, and a Windows Server 2016 VM. I'll be bringing up these VMs on Oracle's Virtualbox. VirtualBox is free, has full support with Vagrant, and there's already a lot of Vagrant boxes already created for VirtualBox.
I'll head on over to the public Vagrant box catalog and pick the three boxes I'd like to use. I'll pick the latest Ubuntu box, a box from Matt Wrock with Windows Server 2012 R2 and a Windows Server 2016 TP5 box. Now that I've selected each of the boxes I'll be bringing up, I'll need to add references to each of the boxes in my VagrantFile.
Assuming you've already got Vagrant and VirtualBox installed, next we need to get into your VagrantFile configuration file. At this point, you've probably already got a VagrantFile created with, perhaps, a single VM that looks something like this:
config.vm.provider "VirtualBox" do |vb| # Display the VirtualBox GUI when booting the machine vb.gui = true # Customize the amount of memory on the VM: vb.memory = "1024" end
If so, we'll have to modify that a little bit to add support for multiple VMs. To do that, we'll have to nest this Ruby do loop inside another to get support for multiple Vagrant boxes. To save you some time, here's the complete VagrantFile I'm working with.
# -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backward compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure(2) do |config| config.vm.define "linux" do |ctl| ctl.vm.box = "ubuntu/trusty64" ctl.vm.hostname = "linux" ctl.vm.network "private_network",ip: "192.168.2.5" ctl.vm.provider "virtualbox" do |vb| vb.memory = 2048 end end config.vm.define "WinSrv1" do |winsrv1| winsrv1.vm.box = "mwrock/Windows2012R2" winsrv1.vm.hostname = "S1" winsrv1.vm.communicator = "winrm" winsrv1.winrm.username = "vagrant" winsrv1.winrm.password = "vagrant" winsrv1.vm.network "private_network",ip: "192.168.2.6" winsrv1.vm.provider "virtualbox" do |vb| vb.memory = 2048 vb.cpus = 1 end end config.vm.define "WinSrv2" do |winsrv2| winsrv2.vm.box = "mwrock/windows2016" winsrv2.vm.hostname = "S2" winsrv2.vm.communicator = "winrm" winsrv2.winrm.username = "vagrant" winsrv2.winrm.password = "vagrant" winsrv2.vm.network "private_network",ip: "192.168.2.7" winsrv2.vm.provider "virtualbox" do |vb| vb.memory = 2048 vb.cpus = 1 end end end
You can see the file that I've arbitrarily chosen is a private IP network of 192.168.2.0, and I have each box set with a different IP address. I've also made sure to configure the Windows boxes to use WinRM with the standard vagrant/vagrant username/password.
Let's now attempt to run "vagrant up" and see what happens!
Notice that even if I don't have the boxes locally, Vagrant will automatically download them from the Internet, perform the necessary configuration, and bring them online. Once I've got my VagrantFile set up correctly, I can simply add a few more lines to this text file to bring up another box immediately.
Now that the machines are up, let's ensure that Vagrant sees them all.
Great! This shows that the three boxes I just provisioned are all online. I'll now try to SSH into the Linux server and use PowerShell remoting to connect to one of the Windows servers.
The box name has been specified
Notice that I had to specify the name of the box. If this were a single machine, I'd simply need to type "vagrant ssh" or "vagrant powershell" instead.
At this point, you're free to test away or do whatever you need to do on these local boxes. When you're done, you can either remove the box, which removes it from your box list, or destroy the box, which also removes the box and all the attached disks as well.
Subscribe to 4sysops newsletter!
Want to write for 4sysops? We are looking for new authors.