Information Technology Reference
In-Depth Information
Write Your First Chef Recipe
To show you the basics, let's write the simplest form of Chef code to make a “Hello World”
recipe
. A
recipe
is a file that contains Chef code.
Using your favorite text editor, create the recipe file
hello.rb
to match
Example 4-1
.
This file
can be anywhere—no specific directory structure is required. By convention, files that con-
tain Chef code have the extension
.rb
to show they are written in Ruby.
The Chef coding language is a Ruby Domain Specific Language (DSL). It contains addition-
al Ruby-like statements specialized for expressing Chef system administration concepts.
Example 4-1. hello.rb
file
'hello.txt'
ddo
content
'Welcome to Chef'
end
end
NOTE
It's not necessary to place
hello.rb
or any of the other *
.rb
example files in this chapter in
a special directory. To find the
hello.rb
file containing the code from the preceding ex-
ample, look among the
source code examples for the topic
in the
chap04/
directory. Other
examples in this and subsequent chapters can be found in similarly titled chapter director-
ies.
We'll go over what all the statements in this file mean in more detail in
Examine hello.rb
.
Enter the code using a text editor, making sure you match the capitalization, spacing, and
syntax exactly.
The
file
statement code you entered in
hello.rb
. is a
resource
. Resources are the building
blocks for assembling Chef code. A
resource
is a statement within a recipe that helps define
actions for Chef to perform. This particular
file
resource in
hello.rb
tells Chef to:
▪ Create the file
hello.txt
.
▪ Write the content
Welcome to Chef
to
hello.txt
.
Use the
chef-apply
command to get Chef to perform the actions indicated in your newly
created
hello.rb
file.