My (very) personal chef tutorial Part IV: Files and definitions

One directory of the cookbook created in part one of this tutorial is the files folder.

The files folder may contain complete configuration item/file for a node the recipe is run on.The basic usage is very simple:

The snippet above from the recipe files-example.rb will copy the file “file.txt” from the “files/default” folder to the “/tmp” folder on the node.

This example can be run by executed the following command line:

Often cookbooks need to be run on different platforms like linux, windows or Mac OS. When including platform specific files, the appropriate file needs to be selected by the cookbook. This can be done by placing the file into a subfolder with the name of the platform as it is specified in the “node[‘platform’]” ohai attribute. When running the recipe with that file in the appropriate subfolder (e.g. “fedora” or “windows”), it will be used rather than the one from default.


Definitons are in essence macros that may be used in all the recipes of a cookbook. Definitions need to be placed in a ruby file in the definitions folder of the cookbook. In contrast to plain recipes, definitions need to be defined including the parameters they may have.

The following example contains a very simple definition that creates a file at a specific location with a specific content:

The marco above has to parameters. One for the path of the file to be created and one for the content to be written into that file.

To use this definition in a recipe, simply use the following syntax:

To run this example use the following command:

That concludes this short introduction into chef files and defintions. In the fifths part of this tutorial we will take a look at using berkshelf to use other cookooks.



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="">