About Resources

[edit on GitHub]

A resource is a statement of configuration policy that:

  • Describes the desired state for a configuration item
  • Declares the steps needed to bring that item to the desired state
  • Specifies a resource type—such as package, template, or service
  • Lists additional details (also known as resource properties), as necessary
  • Are grouped into recipes, which describe working configurations

Where a resource represents a piece of the system (and its desired state), a provider defines the steps that are needed to bring that piece of the system from its current state into the desired state.

The Chef::Platform class maps providers to platforms (and platform versions). At the beginning of every Chef Client run, Ohai verifies the platform and platform_version attributes on each node. The Chef Client then uses those values to identify the correct provider, build an instance of that provider, identify the current state of the resource, do the specified action, and then mark the resource as updated (if changes were made).

For example:

directory '/tmp/folder' do
  owner 'root'
  group 'root'
  mode '0755'
  action :create
end

The Chef Client will look up the provider for the directory resource, which happens to be Chef::Provider::Directory, call load_current_resource to create a directory["/tmp/folder"] resource, and then, based on the current state of the directory, do the specified action, which in this case is to create a directory called /tmp/folder. If the directory already exists, nothing will happen. If the directory was changed in any way, the resource is marked as updated.

Resource Syntax

A resource is a Ruby block with four components: a type, a name, one (or more) properties (with values), and one (or more) actions. The syntax for a resource is like this:

type 'name' do
   attribute 'value'
   action :type_of_action
end

Every resource has its own set of actions and properties. Most properties have default values. Some properties are available to all resources, for example those used to send notifications to other resources and guards that help ensure that some resources are idempotent.

For example, a resource that is used to install a tar.gz package for version 1.16.1 may look something like this:

package 'tar' do
  version '1.16.1'
  action :install
end

All actions have a default value. Only non-default behaviors of actions and properties need to be specified. For example, the package resource’s default action is :install and the name of the package defaults to the name of the resource. Therefore, it is possible to write a resource block that installs the latest tar.gz package like this:

package 'tar'

and a resource block that installs a tar.gz package for version 1.6.1 like this:

package 'tar' do
  version '1.16.1'
end

In both cases, the Chef Client will use the default action (:install) to install the tar package.

Additional Information

See these guides for additional information about resources:

Topic Description
Common Properties Provides a detailed list of the common properties that are available in all resources.
Resource Reference A reference guide that lists both the common and individual options available to every resource that is bundled into Chef.
Custom Resources Shows you how to create your own Chef resources.