Thursday, November 03, 2016

Vagrant using Hyper-V

I have been looking at learning puppet for a while now and to try it out, wanted to quickly deploy a puppet master & node (Ubuntu) on top of my Hyper-V host.

Below are the quick & easy steps I followed mostly for my own reference (n00b alert) :


  1. Enable Hyper-V on the node, this goes without saying :) (reboot after this step).
    001
    002
    003
    004
    #region enable Hyper-V                                                                        
    Add-WindowsFeature -Name Hyper-V -IncludeAllSubFeature -IncludeManagementTools
    #endregion
  2. Install Vagrant using chocolatey.
    001
    002
    003
    004
    005
    #region install vagrant
    Import-Module PackageManagement                                                               
    Install-Package -name vagrant -force -ForceBootstrap
    #endregion
  3. Now in order to use Hyper-V as the underlying hypervisor for Vagrant, earlier we had to install the Hyper-V provider...not anymore. Vagrant supports Hyper-V out of the box as one of the providers. One has to just create the below environment variable to set the default provider as Hyper-V.

    001
    002
    003
    #region set Hyper-V as the default provider for Vagrant
    [Environment]::SetEnvironmentVariable("VAGRANT_DEFAULT_PROVIDER", "hyperv", "Machine")        
    #endregion
  4. Now I created a folder named UbuntuPuppetMaster under VMs folder in my documents directory. I also created an empty file called 'vagrantfile'.

    001
    002
    003
    004
    #region create a directory & file for the Ubuntu (Puppet master) node.
    mkdir C:\Users\Administrator\Documents\VMs\UbuntuPuppetMaster
    New-Item -Path C:\Users\Administrator\Documents\VMs\UbuntuPuppetMaster\vagrantfile -ItemType File
    #endregion
  5. Here comes the power of vagrant to provision VMs, I have to just copy-paste the below content to the vagrantfile created in above step. The vagrantfile is the source of truth for the VM which will be provisioned in next step and you could do lots of stuff here while the VM is being provisioned. Read here more

    001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    VAGRANTFILE_API_VERSION = "2"
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      config.vm.box = "ericmann/trusty64"
      config.vm.provider "hyperv" do |hv|
          hv.ip_address_timeout = 240
          hv.vmname = "Puppetmaster"
          hv.memory = 4096
      end
      config.vm.define "puppetmaster" do |puppetmaster|
        puppetmaster.vm.hostname = "puppetmaster"
        puppetmaster.vm.network "private_network", type: "dhcp"                                  
      end
    end
  6. Now this does not get any simpler, you have to drop into the directory where you have the vagrantfile located in PowerShell and just issue 'vagrant up', it will take care of downloading the vagrant box and provisioning the changes on top of the box.

Note- Vagrant at the moment can't configure networking for Hyper-V provider.

It does not get any simpler than this, now this is a very short post on how to use Vagrant with Hyper-V and there are lot many things you could do with Vagrant, and I would recommend exploring more of it.

No comments:

Post a Comment