WARNING: This article can be outdated. Better read my book about Chef: Cooking Infrastructure by Chef
Hello my dear friends. Today we will continue talk about Chef Solo. All example code you can find here: github.com/le0pard/chef-solo-example/tree/3.0.
In the previous article we learned Chef cookbook structure and wrote own Chef cookbook. In this article we will learn Chef role.
Create your cloud
We learned how to successfully use the Chef to setup servers. In most cases you cloud contain several servers with the same configuration. For example, you can have several web servers and one load balancer, which balance on this web servers. Or you can have several database or queue servers with identical configuration. In this case, it is very hard way to clone each server by nodes, because you need copy all attributes from one node to another. Maintain a system with such nodes also will be hard: you will have to modify the “n” number of nodes to change some attribute value. In this case we need to use the DRY. What we can do? We can use role! A role provides a means of grouping similar features of similar nodes, providing a mechanism for easily composing sets of functionality.
In Chef kitchen we can create roles: web, database and queue roles. Nodes may use one or more roles with recipes. Let’s look at an example.
Let’s create in our kitchen web role. Create in folder “roles” role “web.json”:
The role require such attributes:
- “chef_type” - should be “role”
- “json_class” - should be “Chef::Role”
- “name” - name of role, in our case “web”
- “run_list” - list of recipes, like in node. We just moved run_list from “vagrant.json” node to “web.json”
Also Chef role can have “default_attributes” and “override_attributes”. What’s the difference? “default_attributes” an optional set of attributes that should be applied to all nodes with this role, assuming the node does not already have a value for that attribute. You can override this attributes values in node, which use this role. “override_attributes” an optional set of attributes that should be applied to all nodes with this role, regardless of whether a node already has a value for that attribute. Useful for setting site-wide values that will always be set, because even node attributes cannot override this attributes values. In our case I moved node attributes in “default_attributes” (and update nginx version).
Next, I edited the node for the usage web role:
And do not forget to specify vagrant that we had Chef roles:
Now we can test Chef kitchen with web role:
We should have see the same in your browser by this url “http://localhost:8085/”:
But nginx successfully updated:
Chef very flexible, because your node can use several roles and/or a several recipes simultaneously. For example:
In the current article we have learn the Chef roles. In the next article we will learn more about Chef cookbooks.
All example code you can find here: github.com/le0pard/chef-solo-example/tree/3.0.
That’s all folks! Thank you for reading till the end.