Notes about web and mobile technologies, devops, software architecture, and life

Menu

Provisioning

In this article, I’ll describe an approach to provisioning using Powershell Desired State Configuration (DSC) in Vagrant. I’ll deploy a static website to IIS on Windows Server 2012 to showcase this approach. At the end of the article, I’ve included the final Vagrantfile as well as a DSC Configuration script, but would recommend to read through my steps in order to get a sense on why it was done as such.

On another note, I’m currently employing this setup to develop and test custom DSC modules. Using Vagrant for this purpose has saved me quite a bit of time and given me and fellow developers a nice development workflow.

Pre-requisites:

Create Windows Server 2012 Vagrant Box

The focus of this article is on provisioning. However I’m including a summary of steps taken to create a WS2012 Virtual Box using Packer (For information on manually creating Vagrant Windows Boxes see my previous post)

Clone packer-windows Git Repository and copy your WS2012 ISO to “iso” folder in packer-windows (If you’re not using Git, you can just download the latest release of packer-windows)

Update each builder in packer-windows\windows_2012_r2.json with a relative path to your iso and your respective checksum (I used the SHA1 from MSDN)

If not using the Windows Evaluation ISO, Add your product key to packer-windows\answer_files\2012_r2\Autounattend.xml.

If installing a different Windows Server 2012 other than Standard or Standard Core, you may want to set headless to true in the json file and use the UI to finish the installation (In my case I did this with WS2012 R2 Essentials)

Remove the VMWare builder in the json file if you’re just interested in the Virtual Box VM (or vice versa)

Setup WMF 5.0 and PowerShellGet

In order to use the new Package Manager “PowerShellGet” we need to install the latest WMF 5.0 Preview and configure a PowerShellGet repository. For the purpose of the exercise, we’ll configure the current central repository called “PowerShell Gallery”. These steps assume you have followed the steps in the previous section or you have a windows vagrant box with WinRM communications already configured and working.

Create a new Vagrant project based on your WS2012 vagrant box and enable WinRM

Configure the guest VM to be part of the same Windows Domain as the host or add your host computer as a trusted host on WinRM or use a wildcard (please note using wildcard is not recommended for security reasons):

Create a new DSC/WebSite Project and Initialize Vagrant

Create folder structure for storing your website files and DSC config/mof/custom module files. The structure below is a good starting point, but is only meant to be an example. You may want to do things a bit differently based on your needs.

Using Third-Party DSC Modules

The true power of DSC comes with the large amount of DSC modules that are being published and shared everyday by Microsoft and the PowerShell community. Many of these modules are being uploaded to the central repository: PowerShell Gallery. In this section, we’ll leverage the package manager PowerShellGet to install the xWebAdministration DSC module, that allows us to manage IIS Web Sites, App Pools, etc.

Install xWebAdministration module. We’ll create another provisioner script to take care of any DSC module dependencies for us (similar to how Berkshelf works with Chef, although we’ll just create a simple script to do it).
In the Vagrantfile define the script prior to Vagrant.configure

Note: Get-DscResource is a nice way to confirm you’ve loaded all the necessary modules. It’ll print all modules loaded on the Vagrant output screen. You only have to call it once.
Call the script (prior to any DSC provisioner)

config.vm.provision "shell" do |s|
s.inline = $dscModDepScript
end

Import the DSC module. Now that we have a DSC module installed, we can start using their resources in our DSC configuration.
Import it prior to the Node script block

Import-DscResource -Module xWebAdministration
Node $NodeName {...}

Use any DSC resource from that module. In my case I’ll use the xWebsite DSC Resource. In addition to this resource I’ll use the File DSC resource to copy any files/folders in the MySite folder to the default IIS website.

Run vagrant provision again (or vagrant up if your vagrant VM is down/destroyed)

Test your new site (If using my sample, you should see Hello DSC! by launching a browser on your host machine and going to http://localhost:8080)

Food for thought and final files

As I stated earlier, I used this setup to develop/test custom DSC modules. The dscModDepScript in the Vagrantfile below shows how to install your custom DSC modules (Get-DscResource would take care of loading them as well).

The DSC configuration file might need a bit of tinkering if you were to use it with something like Microsoft Release Management (this file is optimized to work with Vagrant)

The latest version of Vagrant addressed some issues around passing arguments to PowerShell scripts as well as RDP. Highly recommend that you install the latest version, as the sample Vagrantfile would not work. In addition I’ve used all 3 types of shell provisioning (inline, inline+variable, path) and passed arguments to it (as an example of how this works with Vagrant)