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
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
We have a few tasks that need to be completed:
- 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
sudoall the time.
- Modify our group membership so we don't need to run
- 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.
- Create a cookbook called
- Create an attribute called
- Edit our
- Change our driver name so that we're using Docker
- Make it run in a privileged manner
- Set it up so that we don't have to use
- Add a provisioner name and version
- Remove any platforms so that it's just set to work on CentOS 7
- Configure the attribute we created earlier:
- Add a
language, with a value of
- Add a
- Edit our recipe so that our attribute gets installed.
- Verify that the cookbook works
- Log into the Kitchen instance and test for whether or not the Perl language got installed.
- Clean up the environment.
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:
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:
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:
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
SELINUX line from
Create a Cookbook for Use with These Tasks, and Call it
Create the cookbook with this command:
chef generate cookbook la_attributes
Now get into the newly created directory:
Generate an Attribute Called default
Now we can generate an attributes, called
chef generate attribute default
We'll see an
attibutes directory, if we run
Edit Our Kitchen File
We need to change some things in
kitchen.yml, so let's edit it:
In the top section, we're going to replace
docker, and add some things. When we're done, that top section should look like this:
--- driver: name: docker privileged: true use_sudo: false
Remember that this is YAML, so we need to make sure we have the right number of spaces in spots. Those lines under
driver are all indented two spaces.
Down in the
provisioner section, 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
ubuntu from the file. Just delete (or comment out) the line in the
platforms section that reads:
- name: ubuntu-16.04
That's it for
kitchen.yml edits. Write and quit, so that we can get back to the command prompt.
Configure the Attribute We Created Earlier
Add the specific attributes:
default['app']['language'] = 'perl'
Edit Our Recipe
We need to get the
perl package installed, the one we referred to back in the attribute, so add this text to the end of the
package node['app']['language'] do action :install end
Verify That the Cookbooks Work
kitchen verify command to ensure the cookbook works. We should see a line in the output that says yum_package[perl] action install.
Log into the Kitchen Instance to Check That Perl Got Installed
We can log in with
kitchen login. Once we're in, we'll run
perl -v. If we get a version number, then we know Perl has been installed.
Clean up the Environment
To clean up after ourselves, we just use the
kitchen destroy command.
Well, we've done what we set out to do. We took a fresh server, built some framework, and were able to develop a simple cookbook that installed a package with a custom attribute. Congratulations!