Saturday, May 16, 2015

Managing your Azure infrastructure as code - part 3 [Chef cookbooks]


In Chef cookbooks serve as the fundamental unit of configuration and policy distribution. Chef uses cookbooks to perform work and make sure things are as they should be on the node. Cookbooks are the way Chef users package, distribute, and share configuration details. A cookbook usually configures only one service. For e.g. the general structure of a cookbook contains of the elements as given below (attributes, definition, files, recipes etc.)

The important concepts for a cookbook are attributes and recipes. Attributes are defined in the cookbook that can be used to override the default settings on a node. When a cookbook is loaded during a chef-client run, these attributes are compared to the attributes that are already present on the node. Attributes that are defined in attribute files are first loaded according to cookbook order. For each cookbook, attributes in the default.rb file are loaded first, and then additional attribute files (if present) are loaded in lexical sort order. When the cookbook attributes take precedence over the default attributes, the chef-client will apply those new settings and values during the chef-client run on the node. Recipes are Ruby files in which you use Chef's Domain Specific Language (DSL) to define how particular parts of a node should be configured. The default.rb file will be run through first to install the service, followed by module recipes. Inside the recipe the content of the default.rb file looks like:
powershell_script 'Install IIS' do
    action :run
    code 'add-windowsfeature Web-Server'

service 'w3svc' do
    action [ :enable, :start ]

The chef-client will run a recipe only when asked. When the chef-client runs the same recipe more than once, the results will be the same system state each time. When a recipe is run against a system, but nothing has changed on either the system or in the recipe, the chef-client won’t change anything.

Creating and uploading a cookbook

To create a cookbook you can use the generate cookbook command. Login to the chef workstation and use the PowerShell console to navigate to the C:\Chef directory that was created earlier and run the command.
chef generate cookbook [[cookbookname]]

The command will create the cookbook with the files and folder structure for the components for cookbook. These files are generated under the cookbook directory in the Chef directory of the workstation. In our example this is the C:\chef\cookbooks folder.
Next you need to add the commands that will be executed when the cookbook is executed. These commands are stored in the default.rb file in the recipes folder. To add these commands edit the default.rb file in the ~\cookbooks\recipes folder and add the content like
file 'hello.txt' do
  content 'Welcome to Chef'
The cookbook when executed will create a file hello.txt with the content “Welcome to Chef”. To upload the cookbook to the chef server you can use the knife cookbook upload [[cookbookname]] command.

Bootstrapping a windows server

A bootstrap is a process that installs the chef-client on a target system so that it can run as a chef-client and communicate with a Chef server.
Use the knife bootstrap subcommand to run a bootstrap operation that installs the chef-client on the target system and also apply the cookbook on the client machine.

knife bootstrap windows winrm ‘servername’ -x ‘adminuser’ -P 'password' -r "recipe[cookbookname]"

No comments: