Introduction
Just as Puppet, Chef is an automation tool. It can save you time by automating your daily repetetive tasks. In this article I won’t explain in details again terms like imperative and declarative models or idempotency seen together in Puppet articles.
But as a quick reminder, declarative languages like Chef contrast with imperative languages which specify explicit manipulation of the computer’s internal state; or procedural languages which specify an explicit sequence of steps to follow.
Chef is a declarative and also an idempotent language. It means you can run your script multiple times and have the same result and you don’t need to
Chef is a Ruby based, system automation-friendly, IT automation tool used to configure, manage, deploy and orchestrate various applications and services accross your infrastructure.
As Puppet, Chef comes with 2 models : Server client model ou standalone model called Chef Zero used for testing and POC. You can install Chef on several OS such as CentOS, Ubuntu, Mac OS or Windows.
You can automate several tasks with Chef, like installing LAMP components on remote servers, provision VMs or instances on public clouds AWS Azure GCP or private clouds like VMWware or Openstack.
Chef Components and workflow
Chef is a based on pull mechanism as so :
As you can see, Chef architecture is divided in three sections :
- Chef Workstation : local development platform for Chef users to create, test and apply code. You will need chef DK for it.
- Chef Server : responsible of managing the Chef code and acts as a hub for configuration data. It can be Hosted or OnPrem.
- Chef Client / Node : pulls the required configuration and files from the Chef server and applies locally
The Chef Workstation works with an utility called “knife” using RSA key-pair authentication.
Connection between Chef Server and nodes is established through RSA key-pair. Everytime Chef client runs, it gathers information about the node, with a dedicated utility called OHAI and then establish connectivity with the Chef server.
Installation documentation
Please refer to the official documentation here.
I will not treat this point further in this article.
If you want to install Chef in an Hosted mode you can do it through this link. It will provides you a nice GUI where you can see your nodes, reports on tasks, cookbooks, roles, environments and variables :
Chef components
Chef Resources are the fundamental building blocks of Chef code. Resources are the built-in functions that get executed at the backend to perform various operations in Chef.
Multiple resources combined are called “Recipes.” They are named with the extension .rb for Ruby.
Multiple recipes build together are called “Cookbooks“. They are collection of files and directory such as recipes.
Finally a “Run List” can be used to run multiple “Cookbooks.”
Resource examples
In this chapter we will see together examples with built-in resources. FYI, it’s possible to use custom resources you can code in Ruby.
A resource is a block with four components: a type, a name, one ore more properties and one action. The syntax is the following :
type 'name' do
attribute 'value'
action :type of action
end
Here are examples easily understandables.
Example 1 :
# Create a User
user 'lionel' do
action :create
end
Example 2 :
# Remove a file
file '/tmp/test' do
content "This is a test file\n"
action :delete
end
Example 3 :
# Install httpd
package 'httpd' do
action : install
end
Chef Recipe
If you want to use multiple resources in the same file, you can use recipes. You simply have to create a .rb file with you Chef code. Then, you can check your code by using the following command :
cookstyle <RECIPE FILE>
You can then run a dry-run to test your code locally as so :
chef-client --why-run <RECIPE File>
Finally to run to run your code locally, please run :
chef-client <RECIPE File>
If you are using Chef Zero (standalone) add –local-mode for testing and running recipes.
Cookbooks architecture
To create a Cookbook template, just run the following command :
chef generate cookbook <name of the cookbook>
It will create a cookbook squeleton with all directories. You will now be able to create your Cookbook, coding your ruby files.
Let’s see together Cookbooks most important directories and files :
- README.md : this file discribe what the Cookbook is about
- Metadata.rb : this file contains the name of the Cookbook, version number, licence information and description
- recipes : this directory stores all rb files. There is a default.rb file for default config.
Runlists
If you want to use Recipes from Cookbooks in Runlists, you can use the following command :
chef-client --runlist "recipe[Cookbook-Name::Recipe-Name]"
# Example
chef-client --local-mode --runlist "recipe[webserver::default]"
# Example 2
chef-client --local-mode --runlist "recipe[webserver],recipe[firewall]"
That’s all for today !
Sources
docs.chef.io