|
Definitions allow you to create new Resources by stringing together existing resources. I think an example will explain best: apache_site Definition This definition file creates a new resource apache_site. We utilize it by placing: apache_site resource |
Table of Contents |
How definitions work
Definitions have three components.
- The resouce name
- The Prototype Parameters
- The Params hash
You start with define, followed by the name of the new resource (apache_site). What follows is the Prototype Arguments for the definition - these are used to set default values for parameters within the definition. Those parameters are then accessed within your definition via the params hash. Since this is a hash, each parameter must have a value. If no default value is specified, use nil.
| Hey, CGI programmers... The params hash should look familiar to you - it's basically the exact same model we've been following ever since the CGI.pm days. |
When a definition resource is used in a recipe, such as:
We take the attributes of our new apache_site resource, and make them accessible via the params hash. This will be true of any attribute we set - you do not have to prototype all your parameters.
Within the context of the Chef run, this definition will be replaced by all the resources you specify within the definition - in this case, it is expanded to:
You can have as many resources in a definition as you want.
Definitions aren't resources
They are replaced by the resources they contain. This is important. You cannot notify a definition to take an action - you can only notify the resources it creates.
When to use Definitions
You want a definition if:
- You are repeating a pattern of resources
- You do not want to send actions directly to this resource - i.e., you never need to notify it
If you do need to send actions to the resource, you should look in to creating a new Provider.