Thursday, January 15, 2015

Windows box with Vagrant

Some people have been asking me about Vagrant and Windows environments so today, I'm going to write about the easiest way to create and configure Windows 2008 R2 box in Vagrant.
The documentation is very fragmented along the network and I will try to unify it in this post.

First of all, you need to download and install Virtualbox, create a Windows 2008 R2 virtual machine and now you have to configure this VM to run in Vagrant.

So to run Windows in Vagrant you need the following configuration before starting for your base
  • Turn off UAC from GPO 
  • Disable complex passwords <-- It is required by documentation for some reason. 
  • Disable "Shutdown Tracker" 
  • Disable "Server Manager" starting at login (for non Windows Core) 
  • Enable RDP 
  • Create a specific Vagrant user (with administrator permissions) 
  • Install Guest Additions tools 
  • Setup Winrm

To set up Winrm you need to to execute the following commands from Powershell:

winrm quickconfig -q
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="512"}'
winrm set winrm/config '@{MaxTimeoutms="1800000"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'
Start-Service WinRM
set-service WinRM -StartupType Automatic

Now, we have our Windows 2008 R2 box ready to run in Vagrant.

Let's start with Vagrant commands. 
Let's create our .box file:
vagrant pacakge vm-name --output=X:\

The vmname parameter is the name of base virtual machine displayed in Virtualbox.

Open a CMD and create a new directory for  your Vagrant workspace, for example x:\test.
Execute vagrant init inside your workspace directory, this command is going to create the structure required by Vagrant.
With the command vagrant box add X:\ --name vmBASE  you are going to add the box to the local repository.
Now we have to modify the Vagrantfile, which is the key to work with Vagrant, I'm going to paste an example file:

# -*- 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
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|

  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at

  # Every Vagrant development environment requires a box. You can search for
  # boxes at = "vmBASE"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # "private_network", ip: ""

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
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"
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # for more information.
  # config.push.define "atlas" do |push|
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   sudo apt-get update
  #   sudo apt-get install -y apache2

# Port forward WinRM and RDP (changed values to NOT conflict with host)
  config.vm.communicator = "winrm"    
  config.winrm.username = "Administrator"
  config.winrm.password = "YOURPASSWORD" :forwarded_port, guest: 3389, host: 3391 :forwarded_port, guest: 5985, host: 5987, id: "winrm", auto_correct: true
  config.vm.guest = :windows = 15

Once you have your Vagrantfile ready, it is time to execute vagrant up command to provision and run the virtual machine in your virtualbox.
When the machine is booted up, you can login in it via Virtualbox console or via RDP using vagrant rdp command.

I hope this article was useful to you.

No comments:

Post a Comment