Home  > Resources  > Blog

Exploring Chef Recipes and Cookbooks

 
October 1, 2021 by Bibhas Bhattacharya
Category: DevOps

Author: Fahim Javed

Disclaimer- You will need a specific setup in order to follow all the steps in the tutorial. At Web Age, this setup is provided to students by setting up the VMs and sharing them with students. Sharing VMs in the blog is not possible as it is proprietary, but this tutorial will give you a good sense to understand the topic.

In this tutorial, you will explore Chef basics. First, you will run adhoc recipes from Chef-Shell then you will create recipes and a simple cookbook to install/remove packages and manipulate the file system

Part 1 – Launch terminal

In this part, you will launch the Linux terminal.

1. Open the Terminal window:

Alternatively, you can press Ctrl+Alt+T to access the terminal

or

In the taskbar, click Search. In the search text box, type in the terminal and hit the enter key on the keyboard.

2. Switch to the root user:

sudo -i

3. Enter the password.

Part 2 – Using Chef-Shell

In this part, you will use the chef-shell interactive terminal to execute adhoc recipes.

1. Execute the following command to start Chef-Shell:

chef-shell

2. At the chef-shell prompt, enter the following command to switch to the recipes mode:

recipe_mode

Notice the chef prompt shows as follows:

chef:recipe ({version})>

3. Enter the following command:

log 'Hello world'

4. Execute the following command at the chef: recipe prompt to run the previous log command:

run_chef

Notice it shows the following output

INFO: Processing log action write ((irb#1) line 1)

INFO: hello world

5. Next, let’s create and run a recipe that installs the Git package. Enter the following command:

package 'git'

6. Execute the recipe by running the following command:

run_chef

Note: If Git isn’t already installed on the machine, it will be installed. Otherwise, you will see the following message:

apt_package is already installed – nothing to do

7. Type exit to quit from the chef:recipe prompt to the chef prompt.

8. Type exit again to quit from chef-shell to the terminal.

Part 3 – Creating a simple Chef recipe

In this part, you will create a simple standalone Chef recipe that logs the ‘Hello world!’ message on the screen

  1.  Create the workspace directory and switch to it:

cd /home/wasadmin/LabWorks
[This folder was created in a previous lab, if you need to recreate it use the command: mkdir -p /home/wasadmin/LabWorks]

2. Execute the following command to create a directory named recipes:

mkdir recipes

3. Execute the following command to switch to the recipes directory:

cd recipes

4. Execute the following command to launch the text editor to create a file named hello.rb:

gedit hello.rb

5. Type the following text:

log 'hello world!'

6. Save the file.

7. Close the file and return to the terminal.

In the Terminal you may see a warning after saving the file in gedit.

8. Execute the following command to cook the recipe in local mode:

chef-client -z hello.rb

Alternatively, you can use sudo chef-client –local-mode setup.rb

Notice it displays hello world! message on the screen.

Part 4 – Create more standalone chef recipes

In this part, you will create a standalone recipe that downloads and installs cowsay, fortune, and tree packages. It also creates a hello.txt file.

1. Execute the following commands one at a time and verify they aren’t installed:

/usr/games/cowsay
/usr/games/fortune
tree

2. Run “cat /tmp/hello.txt” and verify it displays a message saying the file doesn’t exist.

3. Execute the following command to switch to the Documents directory:

cd /home/wasadmin/Documents

4. Execute the following command to launch the text editor to create a file named cowsay.rb:

nano cowsay.rb

5. Type the following text:

package "cowsay" do
 action :install
end

6. Press Ctrl+O to save the file and name it cowsay.rb

7. Press Ctrl+X to return to the terminal.

8. Execute the following command to cook the recipe in local mode.

chef-client -z cowsay.rb

Alternatively, you can use sudo chef-client –local-mode setup.rb

Notice it downloads the cowsay package and installs it.

If you encounter an error in installing a package regarding force-yes

WARNING: The following packages cannot be authenticated!

cowsay

STDERR: E: There are problems and -y was used without –force-yes

—- End output of apt-get -q -y install cowsay=3.03+dfsg1-6 —-

Ran apt-get -q -y install cowsay=3.03+dfsg1-6 returned 100

This error indicates that your apt-cache is out of date. Run “sudo apt-get update” to refresh your installation package cache. Then rerun the installation that you attempted.

9. Execute the following shell command to verify cowsay is installed:

/usr/games/cowsay  'Hello World'

The following should be displayed:

10. Execute the following command to create fortune.rb to install the *nix standard fortune cookie program:

gedit fortune.rb

11. Append the following text to update the recipe to install the fortune package:

package  'fortune'

Notice there are no do..end keywords. They are not required if you are using default action.

12. Save the file and exit to the terminal.

13. Execute the following command to cook the recipe in local mode:

chef-client -z fortune.rb

Notice it installs fortune package.

14. Execute the following command to verify “fortune” package is installed:

/usr/games/fortune

Notice it displays a random message each time you run it.

15. Execute the following command to utilize fortune and cowsay together:

/usr/games/fortune | /usr/games/cowsay

16. Execute the following commands to create tree.rb to install the tree package:

gedit tree.rb

17. Append the following text to update the recipe to install the tree package:

package  'tree'

18. Save the file and exit to the terminal.

19. Execute the following command to run the recipe in local mode:

chef-client -z tree.rb

Notice it installs tree package.

20. Execute the following command to verify the tree package is installed:

tree /home/wasadmin

Notice it displays a directory tree.

21. Execute the following command to edit createfile.rb:

gedit createfile.rb

22. Append the following text to update the recipe to create hello.txt file:

file '/tmp/hello.txt' do
 content 'Hello World!'
end

23. Save the file and exit to the terminal.

24. Execute the following command to run the recipe in local mode:

chef-client -z createfile.rb

Notice chef created the /tmp/hello.txt file. Chef recipes can be used for creating configuration files, such as app.config, web.config, settings.xml, and other similar files.

25. Execute the following command to verify hello.txt file exists:

cat /tmp/hello.txt

It shows display Hello World!

26. Execute the following command to create a file named remove.rb that will uninstall the previously installed packages:

gedit remove.rb

27. Type following code:

package 'fortune' do
 action :remove
end
package 'cowsay' do
 action :remove
end
file '/tmp/hello.txt' do
 action :delete
end

Notice for packages you use the remove action and for file, you use the delete action. You are purposefully not removing the “tree” package since it will be used later in the lab.

28. Save the file and exit to the terminal.

29. Execute the following command to run the recipe in local mode:

chef-client -z remove.rb

30. Execute the following commands and verify the packages are uninstalled:

/usr/games/cowsay
/usr/games/fortune

31. Execute the following command to verify the file has been deleted:

cat /tmp/hello.txt

32. Lastly, we’ll create a single recipe file from our three installation ruby files:

cat cowsay.rb fortune.rb tree.rb > setup.rb

We’ll use this recipe later when we talk about cookbooks.

33. View the contents of setup.rb and notice it’s the combination of the three manifests that you created previously:

cat setup.rb

Part 5 – Create a chef cookbook and organize recipes

In this part, you will create a cookbook and organize recipes.

1. Switch to workspace directory:

cd /home/wasadmin/LabWorks

2. Create a directory for storing cookbooks:

mkdir cookbooks

3. Review what the chef tool can do:

chef --help
chef generate --help

4. Enter the following commands so git knows who you are:

git config --global user.email "wasadmin@webage.com"
git config --global user.name "Wasadmin"

5. Generate a cookbook:

chef generate cookbook cookbooks/my_cookbook

6. Type yes to continue.

7. Switch to cookbooks directory:

cd cookbooks

8. Get directory list:

ls

Notice there’s my_cookbook directory.

9. Switch to my_cookbook directory:

cd my_cookbook

10. Get directory list:

ls

Notice there are various files and folders.

11. View Policyfile.rb:

cat Policyfile.rb

Notice it contains a source that points to https://supermarket.chef.io by default. That’s where all the packages are downloaded by default.

12. View chefignore:

cat chefignore

Notice it contains various files and directories. These are ignored by the chef-client. You can add more entries to the file if required

13. View metadata.rb:

cat metadata.rb

Notice it contains author name, cookbook name, description, license, and version.

14. View README.md:

cat README.md

Notice you can add a description of your cookbook here.

15. Switch to recipes directory:

cd recipes

16. Get directory list:

ls

Notice there’s default.rb. You can edit this file and define a custom recipe if required.

17. View default.rb:

cat default.rb

Notice it contains comments by default.

18. Switch to recipes directory:

cd /home/wasadmin/LabWorks/cookbooks/my_cookbook/recipes

19. Copy previously created standalone recipes to the recipes directory:

cp /home/wasadmin/Documents/*.rb  /home/wasadmin/LabWorks/cookbooks/my_cookbook/recipes

20. Get directory list:

ls

Notice that many ruby files exist in the directory.

21. Switch to the recipes directory:

cd /home/wasadmin/LabWorks/cookbooks/my_cookbook/recipes

Chef Infra Client expects cookbooks as the root folder for custom cookbooks.

22. Execute the setup.rb cookbook recipe:

chef-client -z -r "recipe"

Notice it installs fortune, cowsay, and tree packages. It also creates a hello.txt file.

You can also use –recipe instead of -r

23. Run tree command:

tree /home/wasadmin/LabWorks/cookbooks/my_cookbook

Notice it displays my_cookbook’s directory tree.

24. Run remove.rb cookbook recipe:

chef-client -z -r "recipe"

Notice it removes fortune, cowsay, and tree packages. It also deletes the hello.txt file.

25. Run default.rb cookbook recipe:

chef-client -z -r "recipe"

Notice it doesn’t do anything since it’s an empty recipe. You will utilize this default recipe later in the lab.

26. Type as many times exit until the Terminal is closed.

Part 6 – Review

In this tutorial, you learned about Chef recipes and cookbook basics.

Follow Us

Blog Categories