Skip main navigation

Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. T&Cs apply

The Chef Unit

.

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:

AZURE PROVISION

The command prompt will then appear:

COMMAND PROMPT

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.

This article is from the free online

Microsoft Future Ready: DevOps Development, Implementation and Azure Automation

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now