Chef – Troubleshooting

Hands-On Lab

 

Length

01:00:00

Difficulty

Intermediate

In this lab we will get familiar with Chef Troubleshooting. First, we are going to install the ChefDK tools on a server, make sure that Docker is working, and ensure that Git is installed and has some basic configuration. We will then go through troubleshooting a Chef cookbook, which will be provided. We need to figure out what it has for problems, and why it won't work. At the end of this hands-on lab, we will have a working cookbook, after we've performed tests with Docker and Kitchen.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Chef - Troubleshooting

Introduction

In this lab we will get familiar with Chef Troubleshooting.

First, we are going to install the ChefDK tools on a server, make sure that Docker is working, and ensure that Git is installed and has some basic configuration.

We will then go through troubleshooting a Chef cookbook, which will be provided. We need to figure out what it has for problems, and why it won't work.

At the end of this hands-on lab, we will have a working cookbook, after we've performed tests with Docker and Kitchen.

Instructions

We have several tasks that need to be completed:

  1. Set up the environment for use with the rest of the tasks by installing Chef, Docker, Git and the Chef gem for Docker:
    • Install version 2.4.17 of the ChefDK tools.
    • Install Docker CE and all that it requires, start it up, then make it start at boot:
      • Modify our group membership so we don't need to run sudo all the time.
    • Install Git, and configure a name, email, and default editor.
    • Install the gem allowing Kitchen and Docker to work together.
    • Set SELinux to allow us to work through the rest of the steps.
  2. Examine the existing cookbook directories sitting in the ~/chef/ directory:
    • la_troubleshooting: The current broken one that we need to fix
    • la_bad: An exact copy of la_troubleshooting, in case we need to start over.
    • la_good: A working "fixed" copy that we can refer to if we're really stuck.
  3. Find out what's wrong with the la_troubleshooting cookbook.
    • Look at the errors we get when we run kitchen converge
  4. Test (in a browser) to make sure the cookbook creates what it's supposed to
  5. Clean up

Logging In

Using the credentials provided in the hands-on lab page, log into the server as cloud_user and we can get going.

On the Provided Server, Install Version 2.4.17 of the ChefDK Tools

We need to download the correct version of ChefDK, which is 2.4.17. In a command line, run:

wget https://packages.chef.io/files/stable/chefdk/2.4.17/el/7/chefdk-2.4.17-1.el7.x86_64.rpm

Now install it:

sudo rpm -ivh chefdk-2.4.17-1.el7.x86_64.rpm

Once it's installed, we need to make sure Chef uses the correct system locations for things:

echo 'eval "$(chef shell-init bash)"' >> ~/.bash_profile
source ~/.bash_profile

Now check to see if it worked with which ruby, and we should see /opt/chefdk/embedded/bin/ruby get returned.

Install What docker-ce Requires on This Server

We need Docker on our workstation, so we will need to install the yum-utils, add the Docker repository, and install Docker.

sudo yum -y install yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum -y install docker-ce

Now let's set Docker to start now, and on system boot:

sudo systemctl start docker
sudo systemctl enable docker

We should also set your user to be able to use Docker without using the sudo command. Once we've done this, we have to log out and back in again:

sudo usermod -aG docker $USER
exit

Use SSH to get back in again, and check to make sure Docker is running:

docker ps

If we can do that without prefacing it with sudo, we're ready to continue.

Install Git and Set Some Global Defaults for Our User, Email Address, and Editor

When we use Kitchen, later in the lab, we need Git to be installed and set up with some basic information. These commands will make that happen:

Install it:

sudo yum -y install git

Configure some Git basics, like the username, email address, and a default text editor:

git config --global user.name "USERNAME"
git config --global user.email "YOUREMAIL@DOMAIN.COM"
git config --global core.editor vim

Note: We can use fake email information, since we don't actually use it for this lab. It just needs to be set or we will get errors later.

Install the Gem Required for Using Docker with the Test Kitchen

Docker requires a gem for this all to work. Install it with this:

gem install kitchen-docker

Update SELinux to Be Permissive

To perform the tasks properly you should change SELinux so that it is permissive:

sudo setenforce permissive

We should also /etc/selinux/config and change things to permissive there too:

sudo vim /etc/selinux/config

Change the SELINUX line from SELINUX=enforcing to SELINUX=permissive

Examine the Three Cookbooks sitting in the ~/chef/ Directory

la_troubleshooting is the current broken cookbook that we need to fix.

la_bad is an exact copy of la_troubleshooting, in case we need to start over or refer back to it.

la_good a working "fixed" copy that we can refer to if we're really stuck.

Work on the la_troubleshooting Cookbook

Let's get in and start looking around for problems and symptoms. Run cd la_troubleshooting to get into the directory, then run kitchen converge to watch what happens.

It fails. If we scroll back up a bit into the logs, we can see a SyntaxError, and the problem appears to be in recipes/default.rb.

Destroy the kitchen (with kitchen destroy) and let's go have a look at our recipe:

vim recipes/default.rb

The error was about the elinks package, but in this file, that section looks fine. The problem is earlier. there's a missing quote in the line: service 'httpd do line. Fix that (adding a single quote after 'httpd), save the file, and try another kitchen converge.

It fails again, doesn't it? Now what's our error saying? Up in the resource declaration, we can see something's wrong with that section of the cookbook. Again, edit the file:

vim recipes/default.rb

If we look in that section, something should stand out. We've declared a path of /varwww/html/index.html, but there's no such place. We're missing a forward slash between var and www. Let's add that in, save the file, and try a kitchen converge again.

It worked!

Test Whether the Problems Are Fixed

Everything looks good, but is it really? It looks like this cookbook is supposed to set up a web server with some content in the index.html page. Does it? Log into the kitchen, and check the file itself, both by just reading it and in a (text-based) web browser:

kitchen login
cat /var/www/html/index.html
elinks http://localhost

If we see SUCCESS, we've succeeded.

Clean up

Get out of the kitchen and destroy it:

exit
kitchen destroy

Conclusion

We took a broken cookbook, tried "cooking" with it a couple of times, and were able to utilize the error messages to help fix the problem. That's what we wanted. Congratulations!