The Chef Unit
Share this post
We’ve been on an Azure journey. We looked at Azure Resource Manager and its architecture and automation.
We then paused at DevTest labs where we created templates and used them in production. In the last section, we stopped outside of the parameters of Azure and looked at automation resources such as Chef, Puppet and Ansible.
This step demonstrates how you can manage your Azure VM configuration with Chef.
What is Chef?
Chef is considered an Automation Resource that provides seamless integration with Azure with the Cloud API.
Chef has three main architectural components: Chef Server, Chef Client (node), and Chef Workstation.
- The Chef Server is the management point, and there are two options for the Chef Server: a hosted solution or an on-premises solution.
- The Chef Client (node) is the agent that sits on the servers you are managing.
- The Chef Workstation is the admin workstation where you create policies and execute management commands. You run the knife command from the Chef Workstation to manage your infrastructure.
There are also the concepts of Chef Cookbooks and Recipes. These are effectively the policies that you define and apply to your servers.
Chef Cookbooks Templates
Chef uses a Cookbook to define a set of commands that you want to execute on your managed client. Creating a Cookbook is straightforward, and you use the chef generate cookbook command to generate a Cookbook template.
Before creating a cookbook, you must do the following:
- Prepare your workstation
- Create a managed Chef account
- Configure the Chef workstation
- Install the Chef Development Kit.
See the page Automating Azure virtual machine deployment with Chef to prepare for creating a cookbook.
This example calls the Cookbook web server for a policy that automatically deploys IIS.
Under your C:Chef directory, run the following command:
chef generate cookbook webserver
This will generate a set of files under the directory C:Chefcookbookswebserver. Next, you need to define the set of commands that you’d like the Chef client to execute on your managed virtual machine.
The commands are stored in the file default.rb. For this example, a set of commands will be defined that installs IIS, starts IIS, and copies a template file to the wwwroot folder.
Modify the C:chefcookbookswebserverrecipesdefault.rb file and add the following lines:
powershell_script 'Install IIS' do
action :run
code 'add-windowsfeature Web-Server'
end
service 'w3svc' do
action [ :enable, :start ]
end
template 'c:inetpubwwwrootDefault.htm' do
source 'Default.htm.erb'
rights :read, 'Everyone'
end
Save the file after you are done.
To generate the template, run the following command:
chef generate template webserver Default.htm
Now navigate to the C:chefcookbookswebservertemplatesdefaultDefault.htm.erb file. Edit the file by adding some simple ‘Hello World’ HTML code, and then save the file. Next, upload the cookbook to the Chef Server so that it appears under the Policy tab by running the following command:
chef generate template webserver Default.htm
Deploy a VM with Knife Azure in Chef
To deploy an Azure VM and apply a Cookbook to install an IIS web service and default webpage, use the Knife Azure server create command:
knife azure server create --azure-dns-name 'diegotest01' --azure-vm-name 'testserver01' --azure-vm-size 'Small' --azure-storage-account 'portalvhdsxxxx' --bootstrap-protocol 'cloud-api' --azure-source-image 'a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-Datacenter-201411.01-en.us-127GB.vhd' --azure-service-location 'Southeast Asia' --winrm-user azureuser --winrm-password 'myPassword123' --tcp-endpoints 80,3389 --r 'recipe[webserver]'
Substitute your variables for the parameters and run the command. Once the command is run, you can view the provision in the Azure portal:
The command prompt will then appear:
After the deployment is complete, you should be able to connect to the web service over port 80. The port was opened when the VM was provisioned with the Knife command.
Share this post
Microsoft Future Ready: DevOps Development, Implementation and Azure Automation

Microsoft Future Ready: DevOps Development, Implementation and Azure Automation

Our purpose is to transform access to education.
We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.
We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.
Learn more about how FutureLearn is transforming access to education
Register to receive updates
-
Create an account to receive our newsletter, course recommendations and promotions.
Register for free