In the previous article we learned Chef roles. In this article we will learn more about knife and cookbooks.
WARNING: No knife configuration file found
Knife also used to communicate with Chef Server, but in our case we don’t have Chef Server. To fix this warning you should create “knife.rb” file (knife configuration file) and add this content:
In this case knife will automatically found this configuration and will not show this warning.
Let’s create node.js recipe in our tomatoes cookbook. Add this content in created file “tomatoes/recipes/node_js.rb”:
Next, we should add default attributes for this recipe. You should create file “tomatoes/attributes/node_js.rb” with content:
And add this in role “web” run_list:
Now you can test this new recipe by “vagrant provision” command. After running this command, the server will node.js:
This node.js recipe will fail on new server, because to install node.js on server should be installed g++ and gcc before running this recipe. For this exist “build-essential” cookbook with recipes. We should add this in top of our node.js recipe:
This cookbook automatically downloaded by nginx cookbook (it is added as dependency in “metadata.rb”).
Now we can use command “include_recipe” in default.rb recipe:
And rollback last run_list (without node_js recipe, default recipe from tomatoes cookbook automatically will execute node_js recipe):
You can also create “metadata.rb” file for your cookbook and add some info about this cookbook:
In the current article we have learn more about Chef cookbooks. In the next article we will learn more about Ohai and how to write Ohai plugin.