Configure Windows with Chef

 

In a previous post we saw how to install and configure our Chef server. Now it’s time to see how to configure our Windows server. Chef cookbooks share the same layout regardless the target operating system.

Cookbooks

In order to achieve this goal we’ll have to install cookbooks, you can find them here.

This will install the Windows cookbook, the baseline for all windows server configurations. The next step is to upload it to your Chef server. This is mandatory even if you work from your server !

Now you have your cookbook installed and available for your configurations! Let’s have a look at this cookbook.

You see resources, attributes, files, libraries, providers & recipes folders. The metadata.json file (which can be named metadata.db) is interesting, it contains all needed information about this cookbook. I won’t parse all the file, but, just take a look at these lines

It contains all dependencies needed by this cookbook to work and the possible target. All we need to do the job. We see that chef_handler is needed for windows cookbook to work, we’ll install it later. Now it’s time to see how recipes contained in a cookbook is executed on a target.

Roles

As i said, roles are needed to execute recipe(s).

Let’s see what are the lines here:

  • name: hmmm… this is the name of this role 🙂
  • description: …
  • run_list: It contains all the recipes that have to be installed for this role
  • default_attributes: here you can set attributes for your recipes, here the accept_eula is needed in order to ou IIS to install for example.

Create the file in ~/chef-repo/roles/iis.rb and now let’s register the role from this file !

If you want a list of all your node to see possible targets, use the following command and select the wanted target where the chef-client has been bootstrapped before

And now, assign the role to it! There is two ways of doing this. The first is

And the second way:

Just add the role[iis] in the run_list part of this json.

Now you know the minimum to push a role to the target. Let’s see what needed to install the IIS feature to our node.

Install IIS Feature

Now let’s see a full example about how installing the IIS Feature and start the service. We have seen previously that in order the get the windows cookbook working with need chef_handler cookbook too, so let’s start by installing this one

Now let’s install iis cookbook, to do this, we have to find the name of this cookbook. In order to search for existing cookbooks, you can use this command

It seems the cookbook we need is iis

Now let’s “upload” them to the Chef Server. Remember that, in the Chef logic, the development and all this stuff is made from workstation 😉

Ok, now our cookbooks are available by Chef Server. Let’s create the role and assign it to the node previously bootstrapped. We created an iis.rb file in this article so let’s reuse it and assign it.

Note that if the role is already added to the node, it’ll just update the node configuration. It won’t create a conflict of throw an error.

Let’s push the role to our target!

I won’t display the whole log, but actions are executed in this order:

  1. Connect to Chef-Client
  2. Loading cookbooks in client cache
  3. Executions of recipes in the run_list order
  4. That’s all!
This is pretty easy isn’t it 🙂

By the way, if your script encounter errors, you can find a debug trace file in C:\chef\cache !

Execute Desired State Configuration

Now the funniest part begins 😀

Chef can execute DSC resources or scripts. Today we will focus on resource. First, let’s install and upload the dsc cookbook, remember all this stuff is beta ware and shouldn’t be used in production!

This cookbook is delivered by few demo files. For this demo i’ll use demo_dsc.rb

hehe another way of downloading cookbook for Chef 😉

Now let’s create our role, we’ll use the recipe present in the cookbook dsc_demo.

Write the file in ~/chef-repo/roles/test_dsc.json

And finally execute the stuff !

Ok our recipe has been executed like you see!

dsc_chef1

I hope you’ll find interest in Chef and Desired State Configuration for your clients or your infrastructure, if you have question feel free to ask with comments or on Twitter.

Next time we’ll talk about automating Azure with Chef!

See ya 🙂