2

Environment:

  • CentOS 6
  • Chef 10

I downloaded the apache2 cookbook from community.opscode then I ran # chef-client in the client node. The installation and start of service were successful.

[2013-03-12T15:30:00+09:00] INFO: Processing package[apache2] action install(apache2::default line 20)

When I go to cookbooks/apache2/recipes/default.rb, there is no line that says action:install I am very new to Ruby so I can't figure out where are the lines of code that are responsible for the installation.

Additionally, if apache2 is being installed, is there some kind of an installation package included inside the recipe? Say, .rpm package? If yes, what is the complete path?

Thanks in advance!

EDIT:

I learned already that the package in

package "apache2" do
   package_name node['apache']['package']
end

has the action :install as default. But the again, I do not what does it install. Where is the installer package itself or is the cookbook the same as the installer?

2 Answers 2

7

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:

  1. We told Chef to manage a package named "apache2".
  2. We told Chef that the name of the package to manage is actually from an attribute `node['apache']['package'].
  3. Given no specific action, Chef will by default install the package.
  4. 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.
2
  • Great answer! This is actually a great walkthrough of the apache2 cookbook, and a good reference for how to write a cookbook with platform specific attributes
    – TrinitronX
    May 29, 2013 at 7:28
  • Yes, thank you very much! This helped and enlightened me a lot!
    – Ella
    Jul 19, 2013 at 2:02
0

On CentOS the default package manager is yum so it will be installed via yum. See the chef package resource doc provider section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.