My (very) personal chef tutorial Part II: Attributes

In this part we will take a look at attributes.

In the first part of this tutorial we created a cookbook using knife. The files created contained a directory called attributes. This is the place to put files containing attributes.

The file default.rb contained in this directory is picked up by chef per default when running a cookbook.

To add a cookbook specific attribute, add the attribute definition to the default.rb:

This defines an attribute in the namespace “cheftutorial” named “name” and assigns a default value to it.

To access the attributes value use the following syntax in recipes:

Since attributes apply to cookbooks using chef-apply will no longer work. An alternative is to use chef-client in local mode. Local mode can be used to run cookbooks from the local file system. In order to run this tutorial cookbook use the following command in the directory cookbooks:

This command line tells chef-client to run the recipe attribute-example form the recipes contained in the cheftutorial cookbook.

The output of the command looks like this:

That completes this basic attributes example. A starting point to learn more about attributes visit chef’s attribute description.

In the next part we will look at using templates which are used to generate files based on the logic contained within the template.

 

4 thoughts on “My (very) personal chef tutorial Part II: Attributes”

  1. Hey. Thanks for putting these tutorials together.

    I’m having a problem with the chef-client command. It can’t find the cookbook. Any ideas? Thanks!

    [2016-01-28T10:47:54-06:00] WARN: No config file found or specified on command line, using command line options.
    [2016-01-28T10:47:54-06:00] WARN: No cookbooks directory found at or above current directory. Assuming /Users/scott/opsworks/cheftutorial.
    Starting Chef Client, version 12.5.1
    resolving cookbooks for run list: [“cheftutorial::attribute-example”]

    ================================================================================
    Error Resolving Cookbooks for Run List:
    ================================================================================

    Missing Cookbooks:
    ——————
    No such cookbook: cheftutorial

    Expanded Run List:
    ——————
    * cheftutorial::attribute-example

  2. Seems strange. The only thing I could think of is that you are starting the chef client in the wrong directory. It might be a good idea to run the command from the parent directory. E.g.:

    /Users/scott/opsworks/misc/chef-examples chef-client –local-mode –runlist “recipe[cheftutorial::attribute-example]”

    Another solution would be to put a “config.rb” file into the folder where you start the chef client that contains the path to the cookbooks:

    cookbook_path [“/Users/scott/opsworks”]

    See also here: https://docs.chef.io/config_rb.html

    Hope this helps.

  3. “Another solution would be to put a “config.rb” file into the folder where you start the chef client that contains the path to the cookbooks”

    That did the trick. Thanks. I had previously installed Chef and (obviously?) it was looking in a different place for cookbooks.

  4. “Another solution would be to put a “config.rb” file into the folder where you start the chef client that contains the path to the cookbooks”

    That did the trick. Thanks. I had previously installed Chef and (obviously?) it was looking in a different place for cookbooks.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">