Opscode
Home     Introduction to Chef     Cookbooks     Blog     GitHub     Tickets 

Definitions

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

How definitions work

Definitions have three components.

  1. The resouce name
  2. The Prototype Parameters
  3. The Params hash
Definition and Prototyping

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:

Definition in a recipe

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:

The resources created by this definition

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.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Mar 18

    Zachary Belzer says:

    It's worth mentioning that this page doesn't note that you are currently (0.8.x)...

    It's worth mentioning that this page doesn't note that you are currently (0.8.x) required to have one definition per file. There's a ticket for this behavior: http://tickets.opscode.com/browse/CHEF-135


Copyright © 2009 Opscode, Inc. All Rights Reserved.