Chef is a rich tool for configuration management. Because Chef offers so many potential configuration options, ops pros can struggle to get started with a Chef configuration.
A complete Chef environment includes a server, a workstation and the managed machines. To keep it simple, this tutorial will run all these Chef components installed on the same machine. Users who attempt this Chef configuration need the Chef Development Kit (Chef DK), containing the Chef client and the server simulation Chef Zero.
Install Chef Development Kit
Chef DK, provided on the Chef website, is platform-agnostic. This article's configuration exercise uses CentOS Linux 7 as the standard platform.
The development kitcomes with a graphical installer for Mac and Windows. Linux users must extract the package and access the Chef knife command-line interface tool. The command chef -v shows current version information.
With Chef DK installed, as shown in the video above, prepare the shell environment by running the chef shell-init command. On Red Hat Enterprise Linux, the command is chef shell-init bash. This command sets the environment variables so that all of the Chef components are visible. Install Git: with yum install git-all. Git requires no additional configuration.
Initialize a Chef repository to store the cookbooks containing the Chef configuration-specific instructions. Use chef generate app myapp --email "firstname.lastname@example.org" to generate it. This command creates a subdirectory with the name myapp, with some files, including the main configuration file called ~/myapp/cookbooks/mypass/recipes/default.rb (see Listing 1).
Listing 1. The default.rb recipe contents include the myapp cookbook and default recipe.
[root@sander recipes]# cat default.rb
# Cookbook:: myapp
# Recipe:: default
# Copyright:: 2017, The Authors, All Rights Reserved.
To the end of this file, add:
file '/tmp/cheffile.txt' do
content 'Chef example file'
The Chef configuration now has sufficient components for a first run. Assuming that the default.rb file is in the local directory, use the command chef-client --local-mode $(pwd)/default.rb to show the Chef client utility working (Listing 2). This output will indicate whether the system created the file successfully.
Listing 2. The chef-client output is verbose.
[root@sander recipes]# chef-client --local-mode $(pwd)/default.rb
[2017-04-05T12:47:57+02:00] WARN: No config file found or specified on command line, using command line options.
[2017-04-05T12:47:57+02:00] INFO: Forking chef instance to converge...
Starting Chef Client, version 12.18.31
[2017-04-05T12:47:57+02:00] INFO: *** Chef 12.18.31 ***
[2017-04-05T12:47:57+02:00] INFO: Platform: x86_64-linux
[2017-04-05T12:47:57+02:00] INFO: Chef-client pid: 24793
[2017-04-05T12:48:15+02:00] INFO: HTTP Request Returned 404 Not Found: Object not found: chefzero://localhost:8889/nodes/sander.example.com
[2017-04-05T12:48:15+02:00] INFO: Run List is 
[2017-04-05T12:48:15+02:00] INFO: Run List expands to 
[2017-04-05T12:48:15+02:00] INFO: Starting Chef Run for sander.example.com
[2017-04-05T12:48:15+02:00] INFO: Running start handlers
[2017-04-05T12:48:15+02:00] INFO: Start handlers complete.
[2017-04-05T12:48:15+02:00] INFO: HTTP Request Returned 404 Not Found: Object not found:
resolving cookbooks for run list: 
[2017-04-05T12:48:15+02:00] INFO: Loading cookbooks 
Installing Cookbook Gems:
[2017-04-05T12:48:15+02:00] WARN: Node sander.example.com has an empty run list.
Converging 1 resources
* file[/tmp/cheffile.txt] action create[2017-04-05T12:48:15+02:00] INFO: Processing file[/tmp/cheffile.txt] action create (@recipe_files::/home/user/Downloads/myapp/cookbooks/myapp/recipes/default.rb line 6)
[2017-04-05T12:48:15+02:00] INFO: file[/tmp/cheffile.txt] created file /tmp/cheffile.txt
- create new file /tmp/cheffile.txt[2017-04-05T12:48:15+02:00] INFO: file[/tmp/cheffile.txt] updated file contents /tmp/cheffile.txt
- update content in file /tmp/cheffile.txt from none to 2ccbda
--- /tmp/cheffile.txt 2017-04-05 12:48:15.657202657 +0200
+++ /tmp/.chef-cheffile20170405-24793-wufglh.txt 2017-04-05 12:48:15.655202665 +0200
@@ -1 +1,2 @@
+Chef example file
- restore selinux security context
[2017-04-05T12:48:15+02:00] INFO: Chef Run complete in 0.194898281 seconds
[2017-04-05T12:48:15+02:00] INFO: Running report handlers
Running handlers complete
[2017-04-05T12:48:15+02:00] INFO: Report handlers complete
Chef Client finished, 1/1 resources updated in 18 seconds
Verify that the file was created in the /tmp directory before moving on.
Chef standardizes configuration management
Sys admins often need to create a vast array of scripts in multiple languages to set configurations. Chef eliminates that inefficiency by standardizing the deployment framework. Chef functions on any OS, on-premises or in the cloud.
Advance the Chef configuration
You have successfully deployed a Chef configuration. The example was simple to demonstrate how to work with Chef. As a Chef user, you will include more instructions in cookbooks to specify exactly what should be happening when working with the tool. For example: These commands install, start and enable the NGINX service:
package 'nginx' do
service 'nginx' do
action [ :enable, :start ]
Chef requires a Chef server to work in production, whether an on-premises server, or Hosted Chef. To use Hosted Chef, set up an account, upload cookbooks to the hosted environment and connect clients. Alternatively, public cloud providers Amazon Web Services and Microsoft Azure offer Chef server to manage IT environments.