Tag Archives: azure

Getting started with Windows and Chef

Start here https://learnchef.opscode.com/quickstart/workstation-setup/ and work through the following sections

– workstation setup
– ignore vagrant/virtualbox for now
– set up your workstation

Confirm that you’ve got git installed

$ which git

Then install chef on your workstation:

$ curl -L https://www.opscode.com/chef/install.sh | sudo bash
$ echo 'export PATH="/opt/chef/embedded/bin:$PATH"' >> ~/.bash_profile && source ~/.bash_profile

– create a hosted chef account

https://community.opscode.com/users/new

– set up your local repo

https://learnchef.opscode.com/quickstart/chef-repo/

follow this up to section Setup your chef-repo

– create or use windows azure/ec2/rackspace account. we’ll use Azure since we’re all windowsy.

Follow tutorial up to section titled How to create and manage a Windows Azure Web Site

http://www.windowsazure.com/en-us/manage/linux/how-to-guides/command-line-tools/

Here’s my script to spin up and tear down groups of vms. You can do all of this via the UI as well but where’s the fun in that. My script is written for OSX/linux but you can do something similar in Powershell.

#/bin/bash

password='p@ssword!'

## get list of vms
# azure vm image list

for i in {101..103}
do
# create command
azure vm create gnslngr${i} a699494373c04fc0bc8f2bb1389d6106__Win2K8R2SP1-Datacenter-201305.01-en.us-127GB.vhd 'Administrator' ${password} --location "West US" --rdp

# delete command
# azure vm delete ${i}
done

azure vm list

#--------------------------------

Now for some of the manual bits per vm. First, enabling winrm. For chef windows work, we’ll do most everything over knife winrm. So first we’ll have to enable that. (Windows Azure team has this working via azure cli but haven’t released it yet… I think…)

# fire up newly imaged windows vm, open up PowerShell and paste in the following to activate winrm

(New-Object System.Net.WebClient).DownloadFile('http://code.can.cd/winrm_setup.bat','winrm_setup.bat') ; .\winrm_setup.bat

BAM — winrm now works.

Now go into the Azure cloud UI, and add a new endpoint for this VM mapping the public IP’s port back to the internal port for winrm. I use 5985 for both (choosing not to obfuscate). EDIT: this works via CLI now, so use the script below to do the same:

#/bin/bash

password='p@ssword!'

## get list of vms
# azure vm image list

for i in {101..103}
do
azure vm endpoint create gnslngr${i} 5985 5985
done

azure vm list

#--------------------------------

Now for some Chef/knife work. We’re going to download a few windows cookbooks that we’ll need pretty much every single time we interact with windows via Chef. cd into the same directory that has your .chef directory from earlier. These commands pull the mentioned cookbooks from the opscode github repository down to your local file system.

$ knife cookbook site install chef_handler
$ knife cookbook site install windows
$ knife cookbook site install powershell

You’ll get annoyed with this as it wants to put this stuff into git and throws an error. Nothing gets done. Why oh why did we do that? Then you’ll shake your fist at the screen and run…

$ knife cookbook site download chef_handler
$ knife cookbook site download windows
$ knife cookbook site download powershell

Now you’ll have to manually tar -xzvf all of those into your cookbooks directory.

Ok, NOW you’ve got all of the other cookbooks that you need on your workstation. We need to send them all up to the chef server:

$ knife cookbook upload -a

Backing up a few steps, the runlist for a given node is the list of recipes that will be applied each run.  We set that on a per node basis.  We can do this via the UI, via initial node bootstrap, or the way we’re going to now — by creating a role that contains the base windows cookbooks that we use.

First we’ll create a json file, windows_host_role.json as follows:

{
"name": "windowshost",
"chef_type": "role",
"json_class": "Chef::Role",
"description": "The base role for all windows systems",
"run_list": [
"recipe[chef_handler]",
"recipe[windows]",
"recipe[powershell]",
"recipe[vim-windows]"
]
}

Then we’ll create the role on the server with the following knife command:

$ knife role from file windows_host_role.json

That’s all the setup we need. We’re ready to start loading the chef client on to nodes and then engage in some converging.

First we bootstrap install the chef client on our first node:

$ knife bootstrap windows winrm mynewvm01.cloudapp.net -x Administrator -P 'p@ssword!' -N nodenameformynewvm01 --run-list "role[windowshost]"

Note, we could’ve done the same thing by sending up the recipes/cookbooks in list but using roles is cleaner. Here’s an example of sending up the recipes though:

$ knife bootstrap windows winrm mynewvm01.cloudapp.net -x Administrator -P 'p@ssword!' -N nodenameformynewvm01 --run-list "recipe[chef_handler],recipe[windows],recipe[powershell]"

Ok, we’re all set on the server. Now you’ll start work on your new cookbook, MyCookbook. We could’ve waited until after we had a first version of this working to do our bootstrap but whatever.

Confirm that you’re in the right dir by asking knife to show you the cookbooks that it can see

$ knife cookbook list

This command dumps out the following:

chef_handler 1.1.4
powershell 1.0.8
windows 1.8.10

Now we’ll use knife to create a base cookbook. The following command will create a skeletal cookbook in your cookbooks directory.

$ knife cookbook create MyCookbook --license mit --email yourEmail@gmail.com --user yourUserName

Note, knife is good about dumping out options. If you want to see all the switches that you can use with cookbook create, type:

$ knife cookbook create

It will error and then tell you what flags are available.

Edit your cookbook (which is a whole other CATEGORY of posts…).

When you get it in a state you like, you’ll need to add it to the node that you’re working on. First upload it to the server

$ knife cookbook upload MyCookbook

Knife will do a simple parse here and complain if it finds any bad syntax before sullying the chef server with your code. Then do the following to add it to your node:

$ knife node run_list add nodenameformynewvm01 recipe[MyCookbook]

Your development workflow with knife then once you start writing code to MyCookbook will be:

1) make changes to your cookbook

2) upload your cookbook to the chef server

$ knife cookbook upload MyCookbook

3) converge the node by remotely invoking the chef-client and testing out your new cookbook.

$ knife winrm 'mynewvm01.cloudapp.net' 'chef-client -c c:/chef/client.rb' -m -x Administrator -P 'p@ssword!'

4) check changes into git

other great reads and references:

http://docs.opscode.com # step by step guide

http://gettingstartedwithchef.com