Originally posted in his Github repo, Michael Hedgpeth came up with this simple exercise that one can follow daily in order to gain proficiency in Chef.
We recommend that you spend one hour a day, getting as far through the kata as you can. After a while, I expect that you'll be able to get through the entire exercise in less than an hour!
Here is an example of a kata repo that follows these exercises. Each branch is a new day's kata.
-
Create a
chef-training
repo on GitHub and clone it locally. -
Create a new branch using today's date in the name to track it.
-
Ensure that you have a code editor with Chef Plugins installed. I recommend Visual Studio Code.
-
Generate a cookbook into the
chef-training
repo -
Make your cookbook only support Ubuntu.
-
Set up test kitchen to run the
default
recipe of your cookbook using Vagrant and VirtualBox. -
Ensure that Nano is installed (in an InSpec test and recipe). Run kitchen converge and verify to ensure this works.
-
For the rest of the lab, create a Test Kitchen workflow that uses the
kitchen create
,kitchen converge
,kitchen verify
andkitchen destroy
commands. Also, usekitchen login
to manually ssh into your Ubuntu machine. -
Create
/var/website
directory. -
Make sure
/var/old-website
directory does not exist. -
Write a file
/var/website/directions.txt
with text "website goes here" in it. -
Write a file
builder.txt
to/var/website/builder.txt
containing the text "[Your Name] built this" where[Your Name]
is a cookbook attribute with your actual name. -
Download the Chef logo into
/var/website
: https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSgQmQ0CYwU3cpFE6gEB82cp6TSIcBJSisax_HVvEfsgYHGBsO8kQ -
When you run test kitchen,
builder.txt
should contain the textTest Kitchen built this
. -
Run the command
echo ran command > /var/website/command.txt
. -
Don't run the command the second time chef converges (i.e. make it idempotent).
-
If the command does run, do a
git pull
of the architect repository into/var/website/architect
(https://github.com/pages-themes/architect). It shouldn't pull the repository every time. -
Refactor your command and pull into a custom resource called
chef_training_website
. -
Make the git repo that you pull an attribute.
-
Write a
MyLogger
class with aLog
method that prepends the messageCHEF TRAINING:
and outputs that to the STDOUT (using puts).
Now that we've had some practice with basic cookbook development, let's get connected to a Chef Server.
-
Create an Ubuntu virtual machine with VirtualBox.
-
Set up an account on manage.chef.io and ensure your keys and knife.rb are available to your
knife
command on your workstation. -
Ensure your
chef-training
cookbook is uploaded to the Chef Server -
Bootstrap your machine running 2 recipes:
chef-training
and theos-hardening
cookbook. You'll need to ensure the other cookbook is uploaded to the Chef Server as well. -
Run
chef-client
on the machine again, noticing that 0 resources are converged the second time.
-
On your workstation, search for all Ubuntu nodes.
-
On your workstation, search for all nodes that match the attribute used to create the
builder.txt
file above. -
Create a data bag
website
with itemmessages
. Inside ofmessages
, have awelcomeMessage
namedWelcome to Chef Learning!
-
In your
chef-training
cookbook, write a file/var/website/welcome.txt
with the welcome message from the data bag. -
Push the updated cookbook to the Chef Server and reconverge, ensuring that the file is there on your Ubuntu VM.
-
Update the data bag to
Welcome to the BEST Chef Learning EVER!
-
Reconverge and see that the file changed.
-
Show the node's run list with
knife
and look it up in the UI. -
Create a role named
security
which includes theos-hardening
cookbook. -
Read over the README of the
os-hardening
cookbook and find some attributes to set. Set those attributes in yoursecurity
role. -
Change the run list on the command line to remove the
os-hardening
cookbook and add thesecurity
role. -
Reconverge and ensure that the behavior is the same.
-
Create a
development
environment that will be assigned to your existing node. It should:- Always run the latest
chef-training
cookbook on the chef server - Run the
1.4.1
version ofos-hardening
cookbook - Have the
builder.txt
sayingDevelopment Built This
- Always run the latest
-
Assign the
development
environment through theclient.rb
on your virtual machine -
Create another virtual machine that will be your "production" machine
- Run a specific version of the
chef-training
cookbook - The
builder.txt
should sayProduction Built This
- Assign the
production
environment through theclient.rb
- Update your
chef-training
cookbook to change the text ofbuilder.txt
. After uploading it to the Chef Server notice that only yourdevelopment
node was updated, but not yourproduction
node.
- Run a specific version of the
For the first badge, you need to understand Automate itself, so this section won't be example driven but more idea driven.
Watch this video to get an overview of Chef Automate.
Now do this thought experiment:
- How would you solidify your cookbook deployment workflow? (if you don't know workflow enough, watch this video)
- How would you see what happens on your nodes?
- How would you scan your nodes with inspec profiles?