Using Chef on Windows is easier than you think
We regularly add servers to our infrastructure to improve performance and reliability, or when machines need to be replaced. Without a good process, adding new machines to production can be a scary experience. Documentation can be outdated, a human can skip a step in a checklist or a different version of a library can be installed. Using Chef allows us to add new servers confidently and quickly.
Subscribe to be notified when we share more behind-the-scenes posts.Chef is a tool that automates configuration, installing packages and almost anything you can think of. We’ve been using Chef for a while for Linux automation, but Chef also has great Windows support. As Postmark is comprised of both Linux and Windows servers, it wasn’t too hard to extend our Chef coverage to Windows to make adding new Windows servers that much easier.
Chef has a lot of features and it can be hard to discover all of them on your own. The following is a list of Chef features on Windows that we’ve found useful.
Regular ol’ Chef #
The same Chef code that you use to create directories, control services and run scripts in Linux already works on Windows. Otherwise, a Windows-specific alternative exists.
#Creates a directory with proper permissions
#http://docs.opscode.com/resource_directory.html
directory 'C:MyDirectory' do
action :create
recursive true
rights :full_control, "Admin", :applies_to_children => true
end
#Restarts the MyService service
#http://docs.opscode.com/resource_service.html
service "MyService" do
action :restart
end
#Outputs contents of C drive
#http://docs.opscode.com/resource_batch.html
batch "Output directory list" do
code 'dir C:'
action :run
end
Installing Windows Features #
The windows cookbook provided by Opscode (the company behind Chef) provides a lot of great Windows specific resources. The one I find the most useful is windows_feature. A Windows feature is an optional part of the operating system that you can download and includes things like IIS, .NET frameworks and many other important components for application servers. To do this via the UI involves several screens and scrolling through lists of checkboxes and sub-checkboxes. With Chef, it’s easy to declare what features you’d like installed. To get a full list of the feature ids, you can run dism /online /get-features
#Install IIS and the Telnet client
#http://docs.opscode.com/lwrp_windows.html#windows-feature
windows_feature "TelnetClient" do
action :install
end
%w{ IIS-WebServerRole IIS-WebServer }.each do |feature|
windows_feature feature do
action :install
end
end
Using Windows installers #
Another useful item in the windows cookbook is windows_package. It makes installing any kind of Windows program very easy to do and supports most of the common installer types such as MSI, InstallShield and others.
#Install Notepad++ using windows_package
#http://docs.opscode.com/lwrp_windows.html#windows-package
windows_package "Notepad++" do
source 'C:tmpnpp.6.5.3.Installer.exe'
action :install
end
The IIS cookbook #
The IIS cookbook is another great resource from Opscode that makes it very easy to automate creating IIS sites, apps, application pools, configuration and more.
#Create an app pool
iis_pool 'my_app_pool' do
runtime_version "4.0"
pipeline_mode :Integrated
action :add
end
# create and start a new site that maps to
# the physical location C:inetpubwwwrootmysite
# and uses the 'my_app_pool' application pool
iis_site 'My Site' do
application_pool "my_app_pool"
protocol :http
port 80
path "#{node['iis']['docroot']}/mysite"
action [:add,:start]
end
Hopefully, this post gives you a taste of how useful Chef can be. If you’d like to learn more about Chef, I’d suggest starting with the content on the Learn Chef site.