Chef is a configuration management tool written in Ruby. Chef is used to streamline the task of configuring and maintaining a company’s servers. Chef can be easily integrated with cloud-based platforms such as Amazon EC2, Google Cloud Platform, OpenStack, SoftLayer and Microsoft Azure to automatically provision and configure new machines.
There are two types of Chef that you probably might heard of – Chef Server and Chef Solo(chef-solo). Chef Server is preferred way to run Chef in a large server farm where each server is communicating to a centrally-managed Chef server. Chef Solo is an open source version of so called chef-client that allows using cookbooks with nodes without requiring access to a Chef Server. Chef Solo runs locally and requires that a cookbook (and any of its dependencies) be on the same physical disk as the node.
Today you’ll see how to create your first simple Chef cookbook.
Installing Chef
Let’s first install Chef on our centos
machine:
[centos@ip-10-0-61-35 ~]$ sudo rpm -Uvh https://packages.chef.io/files/stable/chefdk/2.5.3/el/7/chefdk-2.5.3-1.el7.x86_64.rpm
Let’s check our installed Chef version:
chef-solo -v
Now when we have Chef installed we need to create a directory chef-repo
where our first cookbook will be located. Opscode, Chef developer, provides it. We can simply download chef-repo
with all required file structure.
Setting up chef-repo
[centos@ip-10-0-61-35 ~]$ wget http://github.com/opscode/chef-repo/tarball/master [centos@ip-10-0-61-35 ~]$ tar -zxf master [centos@ip-10-0-61-35 ~]$ mv chef-boneyard-chef-repo-605eeda chef-repo
If we look inside the chef-repo
directory we can see the following:
[centos@ip-10-0-61-35 ~]$ cd chef-repo/ [centos@ip-10-0-61-35 chef-repo]$ ls chefignore cookbooks data_bags environments LICENSE README.md roles
Our cookbook should be created under cookbooks
directory. Let’s call it hello-chef
. I’m going to use chef generate cookbook
command which will create our new cookbook under chef-repo/cookbooks
directory.
[centos@ip-10-0-61-35 chef-repo]$ cd cookbooks/ [centos@ip-10-0-61-35 cookbooks]$ chef generate cookbook hello-chef Your cookbook is ready. Type `cd hello-chef` to enter it. There are several commands you can run to get started locally developing and testing your cookbook. Type `delivery local --help` to see a full list. Why not start by writing a test? Tests for the default recipe are stored at: test/integration/default/default_test.rb If you'd prefer to dive right in, the default recipe can be found at: recipes/default.rb
Writing our simple recipe
[centos@ip-10-0-61-35 cookbooks]$ cd hello-chef/recipes/
Here you will find the default empty recipe file – default.rb
Open it and write some simple chef logic. For instance let’s say we want our cookbook will write a message while running and will create directory named codeflex
on /home/centos/
# # Cookbook:: hello-chef # Recipe:: default # # Copyright:: 2018, The Authors, All Rights Reserved. log 'Hello Chef!!!' directory '/home/centos/codeflex' do owner 'centos' group 'centos' mode '0755' action :create end
Preparation before running Chef
We’re almost ready to run our first Chef cookbook. Only three things left to do:
First, let’s create a directory chef-solo-cache
where Chef will put it’s internal cache files while running:
[centos@ip-10-0-61-35 ~]$ mkdir chef-solo-cache
Ok, now we need to create solo.rb
file under /home/centos/chef-repo/
. This is chef-solo
configuration file which describes cookbook and cache directory location. Write following content in solo.rb
:
file_cache_path "/home/centos/chef-solo-cache" cookbook_path "/home/centos/chef-repo/cookbooks"
The last thing we need is to create a startup.json
file under /home/centos
which describes which specific cookbook we want to run with chef-solo
. Write following content inside startup.json
file:
{ "run_list": [ "recipe[hello-chef]" ] }
Now we’re finally ready to run our cookbook!
Running chef-solo
[centos@ip-10-0-61-35 ~]$ sudo chef-solo -c /home/centos/chef-repo/solo.rb -j /home/centos/startup.json
Chef output:
Congratulations! You’ve just run your first chef-solo
cookbook! 🙂