📜 ⬆️ ⬇️

Chef for 21 days. Part two. Creating and using a cookbook

Hi, Hi, Habrayuzer! Still with us? CHEF is interesting! We continue our voyage to the skill of the automaton warrior, which began in the first part of this article. This article will focus on the first writing experience of a cookbook , recipes , attributes, and patterns .

Before we go further, a small digression on the infrastructure. In the future, we need a couple of computers or virtual machines. Of course, there is VirtualBox, there is Vagrant, but I strongly advise using Amazon Web Services (AWS). The main reason is that AWS is universally used by different companies, and getting to know it is an added bonus to you. There are free images on it that anyone can use. There are many different services and tools that Amazon offers. Some of them are extremely relevant. I believe that this experience is a must-have experience.

Step 5. Welcome to the kitchen, baby



Welcome to the kitchen! In our hands is the most important and indispensable tool - knife !
What will we cook? Of course, our first recipe, and in order not to forget it and use it in the future - we write it in our cookbook . This is where the knife, which can create cookbooks, will help us. Of course, you can recreate the structure of the cookbook manually, but why? Better to get acquainted with the correct structure of the standard cookbook.
The knife cookbook create directive will help us in creation. It will create a folder with our first cookbook in the / cookbooks directory (you can specify its location in the knife.rb configuration file ). Let's take a look at the main content of this folder.
')


Step 6. Tasting time


Well, let's imagine that our cookbook is ready (I will give it an example and describe its use in the next section).
What to do next? How to upload it to the server and send for execution by nodes? Knife will do it for us. In order to successfully carry out further operations, we need the Chef-admin to have a correctly configured knife (the knife.rb file) and an administrator certificate (for example, admin.pem). Next, you need to go to the / chef-repo directory (where our starter kit is, for example) and, while in it, execute commands.
The first step is to make sure that the nodes are registered on the server — the knife node list . As a result, should get the names of the active nodes.
Next, upload our cookbook to the server. It should be located in the directory specified in the knife.rb file’s cookbook_path .
We execute - knife cookbook upload name-of-cookbook . As a result, a message about successful loading and the presence of our cookbook in the list received by the knife cookbook list command.
The next step is to assign the run-list node. Here you can go 2 ways - simple or correct .
Simple - immediately add a recipe to the run-list - knife node edit name-of-node and make text changes to the run-list section, adding the line “recipe [name-of-recipe]” there . And what if there are ten such recipes? Not very comfortable.

The right way is to add a role . The role file is a description of the attributes of the run-list. Including recipes. Accordingly, one role can be assigned to multiple nodes. The role file is located at / chef-repo / roles :

 name "mailer" description "Role for host that will notify us on changes" run_list "recipe[name-of-recipe]" 


In order to upload a role to the server, use the command knife role from file name-of-role-file . After loading - the role can be assigned to the run-list of the node, which we will do.

At this simple process is completed, we are ready to run the client on the site. After that, we will get a node with ( conditionally ) the cookbook implemented on it.

With this, I will finish the second part and will announce the third - more technically-oriented (with more code), which will tell you about the example of using AWS and Chef .

Source: https://habr.com/ru/post/209368/


All Articles