Using Chef to create tasty recipes to manage Windows servers

The popular management configuration tool Chef uses the concept of Infrastructure as a Code and it supports on-premises, cloud, and hybrid environments. Chef works with several operating systems, including Microsoft, Linux (Red Hat, Ubuntu, Oracle Linux, CentOS, Amazon Linux, SUSE), AIX, FreeBSD, and MacOS. Using Chef, an organization can manage tons of servers and automate the deployment process from a single location, using a consistent process without human intervention. Chef makes it easier because it does not require a lot of scripting — just a few lines of code and the “chef” will take care of the changes at the operating system level.

In the Windows world, an administrator is able to manage several areas of the operating system, such as PowerShell, MSI packages, manage folders, files, registry, services, Windows tasks, environment variables, and more. All those areas of the operating system are known as Resources in Chef’s world. They are well documented and the administrator can check all options available for each resource built in on the solution on Chef’s website.

In this article, we will work with Chef Development Kit (Chef-DK), which is installed on a workstation to test the recipes. In a second article, we will apply our knowledge acquired in this article and we will be creating our first cookbook and applying the recipes to a couple of servers.

Another key point when creating recipes is to understand the Ruby language, which is not hard to learn and it is required when working with recipes.

Installing Chef Development Kit (ChefDK)

The first step is to download the Chef Deployment Kit and there are a couple of ways of doing that. We can use the following PowerShell script, which will invocate the installation file from a website, the code is shown below.

. { iwr -useb https://omnitruck.chef.io/install.ps1 } | iex; install -project chefdk -channel stable -version 2.3.4

The second method is downloading and installing it from here. The installation is straightforward — just leave the default settings to complete the process. After installing the product an icon called Chef Development Kit will be displayed on the Desktop.

Chef

Note: When opening for the first time, you may have to change the Execution Policy of your system, running the following cmdlet:
Set-ExecutionPolicy Unrestricted

After opening the Chef Development Kit, which is a PowerShell prompt, we can use the command chef –version to identify the current version running on our workstation.

We are going to write some code and for that we need a powerful editor. My preference is to use Visual Studio Code, which can be obtained here. It is free and works on Windows, Linux, and Mac.

The installation process is straightforward. Just leave the default settings to get the product installed, although I recommend selecting the option to create an icon on the desktop and register the product on the Explorer shell as well.

Chef

In the same workstation, we need to prepare the directory structure to support our upcoming cookbooks and recipe testing that we will be performing. We will be creating two (2) folders, as follows:


mkdir C:\Chef
mkdir C:\Chef\CookBooks


Creating a Folder

One of the first steps is creating a folder. For that, we will create a file on Visual Studio Code called directory.rb, and we will save it under C:\Chef folder. In order to create a directory using Chef, we can add only these lines listed below. The beauty of the automation is that we did not have to specify the actual command on Windows (mkdir or New-Item in PowerShell) to get a folder created.

Keep in mind that we are keeping things simple here. Using Chef we manage permissions, remove folders, and so forth. Another point is that the default value for action is always created, which means that line could be easily omitted from the script.


directory ‘C:\AppX’ do
action:create
end


Chef

We have a simple Ruby file, and running the cmdlet below we will run the file on local mode. The output will describe the recipe (the directory.rb) and its activities, as depicted in the image below.

Chef-client--local-mode .\directory.rb

Chef

Creating and managing a file

A file is another resource available with Chef, and we can manage content and the files itself using it.

In this article, we will make sure that we add a file called app.conf inside of the folder that created previously and we will add a line (server=server.montreallab.info).


file ‘C:\AppX\app.conf’ do
content ‘server=server.montreallab.info’
end


Chef

Understanding Infrastructure as Code

Again, we are really keeping it simple here, but the following example will give an idea how Chef helps an organization keep systems up and running. Let’s assume that an administrator went to one of the servers where we used Chef to configure the app.conf file and changes to a wrong value, something like server57.montreallab.info.

If we run the same cmdlet again, the Chef client will identify that it is not the same and it will overwrite the value, as depicted in the image below. The best practice requires the Chef Client to run every X amount of time on the servers to make sure that they are always consistent.

If we look closely, we can see the previous information server57.montreallab.info, which was removed and the new information added based on our recipe file.

Chef

The same rule applies when we change a configuration on a server farm, and we want to make sure that those changes were applied.

What’s next?

In this article, we had a glimpse how to automate two simple tasks on Windows Server using Chef and its concept of Infrastructure as a Code.

By performing just these two simple tasks, we can see how this will be useful for a range of solutions. For example: if we have several web application servers with static content to be deployed, we could use Chef to create the folders, add the files, and the server will be up and running in no time.

You may be wondering, how about the IIS? That is not a problem. We can automate IIS deployment and control the service status, and we will cover that in a future article here at TechGenix.

Photo credit: Pixabay

About The Author

1 thought on “Using Chef to create tasty recipes to manage Windows servers”

  1. Hi Anderson

    I’m trying to Install a Windows Server2019 EC2 Instance to start with and follow on from there I wanted a Application suite to be automate using Chef. Do you think you can show me or send me some documentation how to start with chef please?

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top