Information Technology Reference
In-Depth Information
$
touch attributes\default.rb
By default, the Chef Development Kit does not create an
attributes
directory until you tell it
to generate an attribute file. Chef Client, on the other hand, always creates the directory, but
leaves it up to you to create the
default.rb
attribute file by hand.
Now, let's set an attribute in our
attributes
file following the form outlined in
Figure 8-1
.
Be-
fore you edit
attributes/default.rb
the file will have no content, as shown in
Example 8-4
.
Example 8-4. chefdk/motd-attributes/attributes/default.rb
default
[
'motd-attributes'
][
'company'
] =
'Chef'
By convention, when attributes are set in a cookbook's attribute file, the values are expected
to be
namespaced
under a top-level key matching the cookbook name. Then all the key/value
pairs are contained within the top-level key; for example, the
default['motd-attrib-
utes']['company']
value is the string
'Chef'
in this example, as the cookbook name in the
attribute uses the
default
precedence level.
Let's also set an attribute value in our recipe, as seen in
Example 8-5
,
following the form in
Example 8-5. chefdk/motd-attributes/recipes/default.rb
node
.
default
[
'motd-attributes'
][
'message'
] =
"It's a wonderful day today!"
template
'/etc/motd'
ddo
source
'motd.erb'
mode
'0644'
end
end
Update the
motd.erb
template as shown in
Example 8-6
.
You can access attributes from any
source under the
node
object: attribute file values, values set in recipes, or values set auto-
matically by
ohai
. They're all just the corresponding
node
values.
Example 8-6. chefdk/motd-attributes/templates/default/motd.erb
Welcome
to
<
%= node['motd-attributes']['company'] %>
<%=
node
[
'motd-attributes'
][
'message'
]
%>
The hostname of this node is <%= node['hostname'] %>
The IP
address of this node is
<%=
node
[
'ipaddress'
] %>