Vagrant for DevOps: Boxes

Boxes can be obtained from Atlas repos , from whatever other repos or created by ourselves with Packer or manually.
Please pay attention that almost all boxes are OK with VirtualBox , but only some can be used with other providers like Parallels, Hyper-V, AWS, etc.
Boxes are stored separately from VMs, in ~/.vagrant.d and during VM deploy process box is cloned to hypervisor default location.


You can download box from Atlas repo using links or short names with vagrant box add command:

Don’t be afraid of names like precise64 , just see description – https://atlas.hashicorp.com/hashicorp/boxes/precise64

You can list boxes that are available locally with command vagrant box list:

Screen Shot 2016-05-22 at 17.33.52

I’ll use my own boxes because of security and compliance and I’ll create them manually.
Here’re general steps to build our own vagrant box running Windows Server 2016 TP5:

1. Create a new VM and disable sharing between host OS and VM, USB, Audio and all other irrelevant options;
2. Install guest tools, all required roles, features, updates and software;
3. Disable password complexity requirements to set well-known password for vagrant user; It can be done with Local Group Policy – (Computer Configuration > Windows Settings > Security Settings > Account Policies > Password Policy > Password must meet complexity requirements);
4. Disable Shutdown Tracker; It also can be done via Local Group Policy – (Computer Configuration > Administrative Templates > System > Display Shutdown Event Tracker);
5. Allow RDP connections; You can use my instruction;
6. Disable UAC and setup WinRM following vagrant guide;
7. Create user vagrant with well-known password vagrant and include it into a local administrators group (it’s not necessary step, you can use default Administrator or whatever other account).

So far we’re ready to build box from VM –
VirtualBox example:

 

For Parallels we need to create metadata.json , which is a box catalog metadata file which is described in vagrant guide.

metadata.json exapmle:

{
  "provider": "parallels"
}

 

Now we can create a box:

 

You can use Creating a Base Box article from Parallels for more details.
After you’ve created box, don’t forget to delete or rename your source Parallels VM.

For Hyper-V use vagrant guide, it will work fine on older Hyper-V servers (and only with first generation VMs), but not with 2016 because of different formats:

Screen Shot 2016-05-24 at 14.14.11

We’ll test our boxes now, and in order to do it we need to create folder for VirtualBox project and another folder for Parallels project.
Vagrantfile will be the same which is really nice:

Vagrant.configure("2") do |config|

    # Box to create VM from
    config.vm.box = "ws2016tp5"

    # Set VM guest OS type
    config.vm.guest = :windows
    
    # Communicator type to use with guest VM
    config.vm.communicator = "winrm"
    
    # Username to use WinRM
    config.winrm.username = "vagrant"
    
    # Password to use WinRM
    config.winrm.password = "vagrant"

    # Set VM network type
    config.vm.network "private_network", type: "dhcp"

    # Set VM hostname
    config.vm.hostname = "DemoParallelsWindowsBox"

    # Set port forwarding 
    config.vm.network "forwarded_port", guest: "3389", host: "3389", host_ip: "127.0.0.1", protocol: "tcp", auto_correct: true
    config.vm.network "forwarded_port", guest: "5985", host: "5985", host_ip: "127.0.0.1", protocol: "tcp", auto_correct: true

    # Set VM CPU & RAM limits
    
    config.vm.provider "virtualbox" do |v|
        v.customize ["modifyvm", :id, "--cpuexecutioncap", "75"]
        v.memory = "1024"
        v.cpus = "1"
    end    
    
    config.vm.provider "parallels" do |v|
        v.memory = "2048"
        v.cpus = "2"
    end

    # Seconds to wait during gracefull halt
    config.windows.halt_timeout = "120"

end

 

We can make sure that vagrant up works for both VirtualBox and Parallels environments:

Screen Shot 2016-05-25 at 12.18.03

You can find more hypervisor-related options for VirtualBox here and for Parallels here.
And don’t forget about Hyper-V, options can be found here and here.

For Windows you can run sysprep:

I don’t like Windows welcome after sysprep, so I use answer file that disables it:

<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
<settings pass="oobeSystem">
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<OOBE>
<HideEULAPage>true</HideEULAPage>
<HideOEMRegistrationScreen>true</HideOEMRegistrationScreen>
<HideOnlineAccountScreens>true</HideOnlineAccountScreens>
<HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
<NetworkLocation>Other</NetworkLocation>
<ProtectYourPC>1</ProtectYourPC>
<SkipUserOOBE>true</SkipUserOOBE>
<SkipMachineOOBE>true</SkipMachineOOBE>
</OOBE>
</component>
</settings>
</unattend>

 

Now our images are ready and we can upload them to Atlas repo:

1. If you don’t have Atlas account you can create new one for free – https://atlas.hashicorp.com/account/new

2. Create new box in Atlas repo:

Screen Shot 2016-05-24 at 20.58.40

Screen Shot 2016-05-24 at 20.59.28

 

3. Create first version of the box:

Screen Shot 2016-05-24 at 21.00.08

 

4. Create new providers and upload suitable box files for both providers (you can make .box from local unpacked box with vagrant box repackage command):

Screen Shot 2016-05-24 at 21.15.49

Screen Shot 2016-05-24 at 21.41.26

 

5. Now let’s release the first version:

Screen Shot 2016-05-24 at 22.04.19

 

6. We can download boxes to make sure that everything has been uploaded fine:

Screen Shot 2016-05-25 at 15.58.30

 

And the last part – we can start, stop and pause VMs not only from hypervisor GUI, but also from vagrant CLI:
vagrant global-status – will show current status of all active environments;
vagrant status – will show status of current environment (one or more VMs);
vagrant up – will create environment according to Vagrantfile;
vagrant reload – will delete current and create new environment according to Vagrantfile;
vagrant rdp – opens RDP to Windows-based VM;
vagrant powershell – opens PowerShell remote session, but only from Windows hosts;
vagrant ssh – will establish SSH session with Linux-based VM;
vagrant snapshot – will list, create, restore or delete VM snapshots;
vagrant suspend – will put environment on pause, so extra disk space will be required to store data from RAM;
vagrant resume – will turn on suspended environment;
vagrant halt – will shutdown environment;
vagrant destroy – will delete environment, but not Vagrantfile and shared stuff;

Screen Shot 2016-05-24 at 21.22.13

You can find a full list of CLI options in vagrant docs.

Vagrant for DevOps table of contents