- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
Cookbook Components - Attributes
In this lab we will install the ChefDK tools on a server. We will need to make sure Docker is working, then ensure that Git is installed and has some basic configuration. Then we'll gain an understanding of Local Cookbook Development by creating a cookbook that uses a custom attribute to install a package. At the end of this hands-on lab, we will have installed and configured ChefDK tools, developed cookbooks, and tested them with `docker` and `kitchen` commands.
Lab Info
Table of Contents
-
Challenge
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.rpmNow install it:
sudo rpm -ivh chefdk-2.4.17-1.el7.x86_64.rpmOnce 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_profileNow check to see if it worked with
which ruby, and we should see/opt/chefdk/embedded/bin/rubyget returned. -
Challenge
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-ceNow let's set Docker to start now, and on system boot:
sudo systemctl start docker sudo systemctl enable dockerWe should also set your user to be able to use Docker without using the
sudocommand. Once we've done this, we have to log out and back in again:sudo usermod -aG docker $USER exitUse SSH to get back in again, and check to make sure Docker is running:
docker psIf we can do that without prefacing it with
sudo, we're ready to continue. -
Challenge
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 gitConfigure some Git basics, like the username, email address, and a default text editor:
git config --global user.name "USERNAME" git config --global user.email "[email protected]" git config --global core.editor vimNote: 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.
-
Challenge
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 -
Challenge
Update SELinux to Be Permissive
To perform the tasks properly you should change SELinux so that it is permissive:
sudo setenforce permissiveWe should also
/etc/selinux/configand change things to permissive there too:sudo vim /etc/selinux/configChange the
SELINUXline fromSELINUX=enforcingtoSELINUX=permissive -
Challenge
Create a Cookbook for Use with These Tasks, and Call It la_attributes
Create the cookbook with this command:
chef generate cookbook la_attributesNow get into the newly created directory:
cd la_attributes -
Challenge
Generate an Attribute Called default
Now we can generate an attributes, called
default:chef generate attribute defaultWe'll see an
attibutesdirectory, if we runls. -
Challenge
Edit Our kitchen.yml File
We need to change some things in
kitchen.yml, so let's edit it:vim .kitchen.ymlIn the top section, we're going to replace
vagrantwithdocker, and add some things. When we're done, that top section should look like this:--- driver: name: docker privileged: true use_sudo: falseRemember that this is YAML, so we need to make sure we have the right number of spaces in spots. Those lines under
driverare all indented two spaces.Down in the
provisionersection, we need to add a name and version. It should look like this when we're done:provisioner: name: chef_zero # You may wish to disable always updating cookbooks in CI or other testing environments. # For example: # always_update_cookbooks: <%= !ENV['CI'] %> always_update_cookbooks: true product_name: "chef" product_version: "13.8.5"We're on a CentOS machine, so we need to get rid of
ubuntufrom the file. Just delete (or comment out) the line in theplatformssection that reads:- name: ubuntu-16.04That's it for
kitchen.ymledits. Write and quit, so that we can get back to the command prompt. -
Challenge
Edit the Attribute We Created Earlier, and Add a default Attribute of app and language with a value of perl
Edit the
attributes/default.rbfile:vim attributes/default.rbAdd the specific attributes:
default['app']['language'] = 'perl' -
Challenge
Edit Our Recipe to Install What Our Attribute Calls For
We need to get the
perlpackage installed, the one we referred to back in the attribute, so add this text to the end of the./recipes/default.rdfile:package node['app']['language'] do action :install end -
Challenge
Verify That the Cookbooks Work
Use the
kitchen verifycommand to ensure the cookbook works. We should see a line in the output that says yum_package[perl] action install. -
Challenge
Log into the Kitchen Instance to Check That Perl Got Installed
We can log in with
kitchen login. Once we're in, we'll runperl -v. If we get a version number, then we know Perl has been installed. -
Challenge
Clean up the Environment
To clean up after ourselves, we just use the
kitchen destroycommand.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.