Automation

Automating a Home Lab Windows Server with Chef

In a previous post, Basic Chef Workstation and Server Installation and configuration, we took a look at how to get up and running in a basic sense with a working Chef server as well as a management workstation running the Chef Development Kit. However, let’s take a look at how we can move to the next step – actually writing a recipe, uploading a cookbook, bootstrapping a Windows host, and then syncing our host against the assigned cookbook. Of course, I am running Chef in a home lab environment as it is a great way to learn and play around with new technologies. Let’s look at Automating a Home Lab Windows Server with Chef.  Note, the steps below apply equally for the most part with a Linux node as well.  However, in my home lab environment, I am chiefly interested in automating Windows servers in the environment, so that is the use case I am addressing in this post.

Creating Cookbooks

We have already looked at getting your management workstation connected to the Chef server.  This involves creating a knife.rb file and downloading the private key from your Chef server.  Take a look at the last post on how to do this if you are not to this step already.  Once we have the connectivity between the workstation and server configured, we can start creating cookbooks to use in automating various configuration on a Windows server.

Chef uses the whole analogy of food throughout the solution if you can’t tell already.  Cookbooks are what contain recipes.  We can aggregate recipes in our cookbooks and assign these cookbooks to our Windows server.

We can create a plain vanilla, blank cookbook without any configuration with the following command from within our Chef Development Kit environment on the workstation.

chef generate cookbook <yourcookbookname>
Generating-a-new-Chef-cookbook-for-automating-Windows
Generating a new Chef cookbook for automating Windows

We can navigate to our cookbooks directory and see the folder structure that is generated with the new cookbook creation.

New-Chef-cookbook-folder-structure
New Chef cookbook folder structure

To quickly get code into the cookbook, we need to edit the default.rb file of the cookbook that is found under the recipes folder.  We place automation directives in this folder and we can implement the desired state of the server based on this file.

Editing-the-default.rb-file-for-automation
Editing the default.rb file for automation

For example, let’s say we want to make sure our Windows servers has .NET 3.5 installed, we can use the following code snippet placed in the default.rb file.

powershell_script '.NET' do
  code 'Add-WindowsFeature NET-Framework-Features'
  guard_interpreter :powershell_script
  not_if "(Get-WindowsFeature -Name NET-Framework-Features).Installed"
end

Save the default.rb file.  We are now ready to upload the cookbook to the Chef server.

Uploading Cookbooks to the Chef Server

Now that we have a cookbook with code in the default recipe file, we can upload the cookbook to the Chef Server.  Again from the Chef Development Kit on the workstation, run the following command.  This copies the cookbook from the local storage on your Chef Development Kit workstation and uploads it to the Chef server.

knife cookbook upload <yourcookbookname>
Uploading-a-cookbook-to-the-Chef-server
Uploading a cookbook to the Chef server

Bootstrapping a Windows Host and Assigning the Run List

One of the important first steps when managing a Linux or Windows host with Chef is called bootstrapping.  What is bootstrapping in Chef?  Bootstrapping a node includes the following steps:

  • Setting up the chef-client on the node
  • Associating the node with the Chef server for management
  • Applying a cookbook via a run list

Bootstrapping essentially rolls all three of those steps into one process and streamlines getting the node in a state of being managed from the Chef server.  From the Chef Development Kit on the workstation, we can bootstrap a selected host using the following command line directives.

knife bootstrap windows winrm <nodeFQDNorIP> --winrm-user <user> --winrm-password 'password' --node-name <nodefriendlyname> --run-list 'recipe[testcookbook]'

Below, I had already bootstrapped the host before, so it asked me to overwrite it and the client for the node.

Bootstrapping-a-Windows-node-with-Chef
Bootstrapping a Windows node with Chef
After-bootstrapping-a-Windows-node-and-successfully-running-a-Chef-cookbook
After bootstrapping a Windows node and successfully running a Chef cookbook

After running the cookbook, checking the Windows server reveals that .NET 3.5 was indeed installed.

NET-3.5-is-installed-after-running-the-Chef-cookbook
NET 3.5 is installed after running the Chef cookbook

Chef Resources

There are a lot of great Chef resources, however, Chef’s website is a great place to start.  Below are two of the URLs there you need to visit both to learn and to see and download cookbooks that have already been written.

Learning – https://learn.chef.io/

SuperMarket (great name) – https://supermarket.chef.io/

Thoughts

Chef is a great tool to automate in guest operations and perform configuration management, desired state configuration, and alleviating configuration drift.  It is especially fun to play around with in a home lab environment to automate virtual machines that you are quickly provisioning and tearing down.  In the next Chef post, we will take a look at more advanced configuration tasks and management using Chef.

Subscribe to VirtualizationHowto via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com and has over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, Brandon has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family.

Related Articles

Leave a Reply

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

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