You can find the files for this step in the following git hub repository:
Chef is used to describe the steps of the provisioning. This description comes from „cookbooks“ and these cookbooks contain recipes that describe the provisioning steps with ruby code.
So let’s start with a simple cookbook, that adds a user at the end of the provisioning just to get used to the structure of chef cookbooks.
First we need to download and install chef
Then we need to adapt the Vagrantfile to use chef as provisioner and trigger our first recipe in our first custom cookbook.
As you see below, we specify a cookbook_path variable, with an array of directories. In our example we have:
Technically chef is using a fallback here, cookbooks in the seconde folder have a higher priority as in the first folder. With 'chef.add_recipe "mycookbook"' we defince that "default.rb" in our cookbook ist the entry point for the provisioning process.
For our example we create the following file structure in our vagrant folder:
This is the minimalistic example of a chef cookbook. To play arround, we add the following content to „metadata.rb“
We add the following content to the Chef recipe "repices/default.rb":
Now we can create our box a usual with „vagrant up“. When the provisioning is done, we can log into the box (with „vagrant ssh“). Now check „/etc/shadow“ if our new user „testuser“ has been created.
With the blocks above you use „chef resource providers“. There are a lot of build in resource providers that help you to automize steps during the provisioning. You can find the documentation of the build in chef resources here:
As you can see chef provides very helpful „building blocks“ to configure your machine. In addition it is possible to implement these „resources providers“ in your own cookbooks and these concepts make chef very powerful.
We learned how to create an own cookbook and how to use recipes and resource providers, but to build a box it makes senses to use an combine other existing cookbooks. In the next step we will learn to use the apache cookbook to create a virtual host in our development system.