Sex, drugs and sausage rolls

Tech and Life… as seen by Tallmaris (il Gran Maestro)

Vagrant with Windows Support!

In a previous article we explored a convoluted way to allow Vagrant to manage windows guest machines.

As pointed out by a comment, Vagrant new version (1.6+) has native windows guest support, which means that most of the steps are not needed. Let’s quickly explore this new feature.

Setup guest

Nothing complicated to do here, just create a windows VM in Virtual Box and you are done. Remember to create an admin user for Vagrant to use.

Packaging the box

Of course you don’t need the windows plugin now, so you should jump straight into packaging:

vagrant package --base <<vm-id>> --output C:\Vagrant\win7dev.box

vagrant box add <<boxname>> C:\Vagrant\win7dev.box

vagrant init <<boxname>>

Where vm-id is the id of the box in VirtualBox (which should be running when you run the package command).

The Vagrantfile

This is the biggest change, just a few commands to enable remoting into our Windows guest:

# -*- mode: ruby -*-
# vi: set ft=ruby :
 
VAGRANTFILE_API_VERSION = "2"
 
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "Win7Dev" # the name of the box, completed for you by vagrant init

  # specify to use winrm rather than ssh
  config.vm.communicator = "winrm"
 
  # forward RDP port
  # use "host: 3377" if you are running locally, 
  # since 3389 will be already taken by your RDP listener.
  config.vm.network :forwarded_port, guest: 3389, host: 3389 
end

Save the file and run:

vagrant up

Note that all the provisioning we did already in Puppet and Chocolatey is still valid.

Accessing the machine

You should see the machine running in Virtual Box, but how do you access it? This is the other big change in Vagrant 1.6+, a new command for remoting into a Windows guest:

vagrant rdp

There you have it! The only difference is that you will have to enter the password manually since it cannot be sent with the connection command.

Please note that version 1.6.3 has a little bug in the rdp.rb file. If you get an error regarding that file, simply find it in your Vagrant installation folder (into plugins/hosts/windows/cap/) and apply the changes from commit 1b32cba:

  # at line 24:
  f.puts("#{k}:#{v}")

  # at line 29:
  args = [config_path.to_s]

Of course, remember to have RDP enabled and the firewall open on the guest machine. I have started to use this simple provisioning script in my Vagrantfile:

  $script = <<SCRIPT
    reg add "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
    netsh advfirewall firewall set rule name="Remote Desktop (TCP-In)" new enable=yes
  SCRIPT

  config.vm.provision "shell", inline: $script

That should be all. Happy coding!

, ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.