There's a few things to talk about here. First, the canonical reference documentation is on the Chef Docs page for package resources.
Since Chef recipes are a Ruby DSL, let's examine what that means. Each resource defined in a recipe has a type, which can have one or more providers. The resource is a declarative interface to the underlying thing that handles how to manage that resource, the provider. Chef chooses the provider automatically based on the node's platform. In the case of package, Chef has providers for yum, apt, solaris, macports and many more. See the documentation for information.
When you declare a resource in a recipe, it takes two arguments in Ruby terms, a string and optionally a block. The string is the resources "name". Each of the attribute parameters in the block may have default options, which are documented on the docs site for the various core Chef resources and providers. The name is also used for one of the attributes, called the "name_attribute". In the case of this example:
package "apache2" do
package_name node['apache']['package']
end
The name of the resource is "apache2" for all intents and purposes. However, the name attribute for packages is package_name
, which in this case the package name comes from the attribute, node['apache']['package']
, which is set in the cookbook's attributes/default.rb
file. This is platform specific since no two distributions/OSes can agree on what to call the package (apache2 on Debian, httpd on RHEL).
A special attribute for resources is the action. This tells the underlying provider what state the resource should be. Chef will take the most positive action for resources by default. As a declarative interface to the underlying system resources, Chef considers that to be the most sane unsurprising thing. In the case of a package, Chef will by default install the package.
So as an overview in writing this kind of recipe:
- We told Chef to manage a package named "
apache2
".
- We told Chef that the name of the package to manage is actually from an attribute `node['apache']['package'].
- Given no specific action, Chef will by default
install
the package.
- Chef will use the underlying package manager set by default for the node's platform to install the package. On Debian/Ubuntu systems, it will do
apt-get install apache2
. On RHEL/CentOS etc systems, it will do yum install httpd
.