Day 57: Chef: a simple Recipe and Cookbook for Windows

In last week’s post, Bootstrapping the Chef Client onto a Windows Host, we demonstrated how to deploy the Chef Client onto Windows Server 2012 R2. This week, we are going to get started with a sample Chef recipe for Windows.

Please note that you can use a Domain Account to complete the walk-through below; however, the account must have Administrator rights locally on the Windows Host.

 

Before we begin, there are a few prerequisites we must go through first.

 

Install the Chef Development Kit

As of this writing, the current Chef Development Kit is at version 0.3.0. The size of the package is roughly 200 MB and shouldn’t take longer than 5 minutes to install on your Windows Server 2012 R2 Host. You can download the current version from here.

 

Configure your Windows Host to report to your Chef Server

One thing I forget to mention last week was how to setup the Chef Client in the Windows Host as a Service so that it reports back automatically to your Chef Server on a regular basis. Run the following commands below to set this up:

 

You can check to see if the Chef Client Service is operational by running the following command:

 

 

Setting up a Chef Recipe and Cookbook

Open up a PowerShell prompt, and run command below; note that you should be in  C:\Users\<Username>

 

The Recipe and Cookbook that we are creating in this post will be stored in chef-repo directory.

Next, cd into in your chef-repo directory and run the following command to create a place to store your cookbooks.

 

cd into the cookbooks directory.

Next, run the following command to generate a template cookbook where we will be storing a recipe for installing and configuring IIS on our Windows Server 2012 R2 Host.

 

Once the cookbook template has been generated, cd to C:\Users\<Username>\chef-repo\cookbooks\recipes and open up default.rb with a text editor of your choosing. Paste in the following configuration:

The configuration above will do the following:

– Install IIS (Web-Server) if it is not already installed.
– Install the IIS Management Console (Web-Mgmt-Console) if it is not already installed.
– Start and enable the IIS Service (W3SVC)
– Set the Default.htm webroot page to whatever is configured in our template HTML file, index.html.erb

 

Next we need to configure the template HTML file mentioned above; so, cd to C:\Users\<user>\chef-repo\cookbooks\W2K12R2_IIS\templates\default and open up index.html.erb with a text editor of your choice.

Paste in the following configuration

 

Now that we have everything in place, we need to apply the configuration changes to the Windows Server by running the chef-client command below:

 

Once the command has finished running, you should be able to open up the IIS Management Console (inetmgr from the run box) and verify that IIS is running by Browsing to the default web page. Additionally, you should see a message about the server being managed by Chef on the default web page.

If you run the command above again, you’ll notice that it will skip the installation of the IIS Web Server and the IIS Management Console due to the not_if statements in the recipe. After the command has completed running, it will always notify you of how many resources have, or have not, been updated and what those changes are. The easiest way to see this is to open up the index.html.erb file, add some additional text and then run the chef-client command above again.

Side Note: When you want to run a cookbook, as we have in this example, you use the chef-client command; however, if you want to run a single recipe, you use the chef-apply command.

 

 

Conclusion

This week’s post has demonstrated how to setup and configure a Chef Recipe and Cookbook on Windows Server 2012 R2 and then apply it to the Host. In next weeks post, we’ll explore more about managing Chef Client Configurations from the Chef Server.

 

Previous Installments

100 Days of DevOps with PowerShell

 

 

 

 

 

 

 

 

 

 

 

 

Leave a Reply

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