Recipes are a collection of resources, written in Ruby. Chef doesn’t have a custom DSL to learn(yay!). It doesn’t take a full fledged Ruby developer to write code for Chef, understanding the basics will be more than enough to functions productively while using Chef.
Basic programming principles can also be applied to recipes. This means that loops, if statements, case statements, etc, can all be used in recipes to help configure nodes. For example, we can use if or case statements to install a package on a node based on it’s platform(e.g. httpd vs apache).
A recipe as defined by Chef is:
The most fundamental configuration element within the organization. A recipe:
- Is authored using Ruby, which is a programming language designed to read and behave in a predictable manner
- Is mostly a collection of resources, defined using patterns (resource names, attribute-value pairs, and actions); helper code is added around this using Ruby, when needed
- Must define everything that is required to configure part of a system
- Must be stored in a cookbook
- May be included in a recipe
- May use the results of a search query and read the contents of a data bag (including an encrypted data bag)
- May have a dependency on one (or more) recipes
- May tag a node to facilitate the creation of arbitrary groupings
- Must be added to a run-list before it can be used by the chef-client
- Is always executed in the same order as listed in a run-list
Resources are executed in the order they are listed within a recipe, starting with the first recipe in a run list.
It is important to note that the subscribes and notifies properties can influence the order in which resources in a recipe are executed.
As part of the Chef definition of a recipe, you may have noticed that a recipe May be included in a recipe. This can be a recipe from an external cookbook. The syntax for this would be:
Which will include the specified recipe in your recipe. You can also use the following syntax:
Which will include the default recipe in that cookbook.
The benefit of this is that you can take advantage of pre-existing recipes, cutting down on your development time.
For more information on recipes, see the official Chef Documentation
Comic courtesy of Sinfest