Glen Knight

NYC Based IT Professional

Chef Resources Overview

resources
A resource is a desire state of configuration for a given item, and the code block describes the desired state and steps for achieving the desired configuration.

Resource are managed within “recipes” and are generally grouped together within cookbooks for specific software and tasks.

A resource maps to a “provider”, which defines the steps to achieve that desired state of configuration.

Chef is idempotent, which means during a Chef convergence, Chef tests if the desired configuration within a resource is set. If it is, Chef does not run anything against the node. If the desired configuration doesn’t match, then Chef enforces(repairs) the desired state of configuration.

A resource has four components and is specified by a ruby block of code:

  • Resource Type
  • Resource Name
  • Resource Properties
  • Actions to be applied to the resource

Packages

package ‘httpd’ do
  action :install
end

If we did:

package ‘httpd’ do
end

It would still install the httpd service, since the default action is :install. It’s important to note that due to Chef’s idempotentcy, the package will only be installed if it is not already installed. And if this recipe is run repeatedly, the package would only be installed, if its already not; otherwise, nothing would happe.

Package Actions

:install
Will install a package(default action if not already defined)

:nothing
This action does nothing until notified by another resource to perform an action

:purge
Removes the configuration files as well as the package(Debian Only)

:reconfig
Reconfigures a package

:remove
Removes a package and configuration files
:upgrade
installs a package, and if it’s already installed, ensures it’s at the latest version

Services

service ‘apache’ do
  service_name ‘httpd'
  action [:enable, :start]
end

In the service resource, the service_name property is what names the actual package to be installed. If this property is missing, the resource name is used as the service name. The default action of a service resource is :nothing. This means that if no action is specified, the resource will only exist in the code until a notify action is sent to it.

Service Actions

:disable
Disable a service so it does not start at startup

:enable
Enables a service to start at system start

:nothing
Does nothing to the service(default action)

:reload
Reloads the configuration for a service

:start
Starts the service, and keeps it running until stopped or disabled

:stop
Stops a service

When a change is made and requires a service restart(configuration changes, for example) we can use the notify property to trigger another resource when the state changes

Files

file ‘/etc/httpd/conf/httpd.conf’ do
  content ‘blah blah content’
  notifies :restart, ‘service[httpd]’
end

The file resource type is used to create and update files on the file system. The default action is :create.

In the above example we are creating(actually updating the file as the file should already exist) a file at the path /etc/httpd/conf/httpd.conf, which is the default configuration file for the httpd service. We are specifying the content of the file to be ‘blah blah content’. If changes have been made to the file, we are notifying the service to restart. It is important to note that the service being notified in the file resource should match the resource name of the service resource. To drive that point home, take this recipe:

service ‘apache’ do
  service_name ‘htpd'
  action [:enable, :start]
end

file ‘/etc/httpd/conf/httpd.conf’ do
  content ‘blah blah content’
  notifies :restart, ‘service[httpd]’
end

This won’t work, as the resource name specified in the file resource does not match the name of the service resource. We can correct this, as seen below:

service ‘apache’ do
  service_name ‘htpd'
  action [:enable, :start]
end

file ‘/etc/httpd/conf/httpd.conf’ do
  content ‘blah blah content’
  notifies :restart, ‘service[apache]’
end

This recipe is now correct. The service resource will install, enable, and start the service httpd, and then the file resource will modify the file content, and notify the service resource that the specified service should be restarted.

This covers a few of the most commonly used resource types. A more comprehensive list of resources can be found in the official Chef Documentation

Leave a Reply

Your email address will not be published. Required fields as marked *.