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.
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>
We can navigate to our cookbooks directory and see the folder structure that is generated with the new cookbook creation.
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.
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>
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.
After running the cookbook, checking the Windows server reveals that .NET 3.5 was indeed installed.
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/
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.