What are Attributes?
Attributes represent information about your node
§ The information can be auto-detected from the
node (e.g. # of CPUs, amount of RAM) &
populated by Ohai
§ You can also set attributes on your node using
cookbook recipes & attribute files, roles,
environments, etc
§ Attributes keep the program code separate from
data
§ All attributes are set on the "node object", and
are indexed for search on the server.
Attribute Sources
Attributes can be set at various levels (in
increasing order of precedence)
− Automagically on the node itself (by Ohai)
− In roles
− In environments
− In cookbook recipes
− In cookbook attribute files
Setting attributes in attribute files
Attributes can be set in the cookbook's
attributes file
− ./cookbooks/<cookbook>/attributes/default.rb
Format is
precedence attribute name attribute value
default["apache"]["dir"] = "/etc/apache2"
Setting Attributes in Recipes
They can also be set directly in Recipes
Precede attribute name with 'node.' as
follows
precedence
as a method attribute name attribute value
node.default["apache"]["dir"] = "/etc/apache2"
the node
object
More on attributes - Advanced!
An attribute value can be an array
default["apache"]["listen_ports"] = [ "80","443" ]
Or it can be a hash
default["apache"]["site1"] = { "port" => 80 }
default["apache"]["site2"] = { "port" => 81 }
8
Never use array attributes
A terrible idea, nipped in the bud
(its a trap)
important point:
It could be a hash. Hashes are great
default["apache"]["site1"] = { "port" => 80 }
default["apache"]["site2"] = { "port" => 81 }
The Problem and the Success Criteria
§ The Problem: We have defined our
'index.html' homepage in the recipe, but we
may want to change that without altering the
recipe
§ Success Criteria: We can change the
homepage by changing an attribute value
Exercise: Set attribute in attributes file
OPEN IN EDITOR: cookbooks/mysite/attributes/default.rb
default['mysite’]['indexfile'] = 'index1.html'
SAVE FILE!
Set an attribute to a specific value in the attributes
file
Exercise: Add index1.html to cookbook’s files/
default directory
OPEN IN EDITOR:
cookbooks/mysite/files/default/index1.html
<html>!
<title>Better Homepage for Gap, Inc.</title>!
<body>!
<h1>Welcome the Gap!</h1>!
<h2>Let’s sell some pants and shirts!</h2>!
<p>We configured this in the attributes file</p>!
</body>!
</html>!
Exercise: Set attribute in a recipe
OPEN IN EDITOR: cookbooks/mysite/recipes/default.rb
service "httpd" do
action [:enable, :start]
end
cookbook_file '/var/www/html/index.html' do
source node['mysite’]['indexfile']
mode '0644'
owner ‘nobody’
group ‘nobody
end
Exercise: Upload the cookbook
knife cookbook upload mysite
Uploading mysite [0.1.0]
Uploaded 1 cookbook.
Exercise: Set attribute in the recipe
OPEN IN EDITOR: cookbooks/mysite/recipes/default.rb
service "httpd" do
action [:enable, :start]
end
node.default[’mysite]['indexfile'] = 'index2.html'
cookbook_file '/var/www/html/index.html' do
source node[’mysite’][’indexfile’]
mode '0644'
owner ‘nobody’
group ‘nobody’
end
Exercise: Add index2.html to cookbook’s files/
default directory
OPEN IN EDITOR:cookbooks/mysite/files/default/index2.html
<html>
<title>Better Homepage for Gap,Inc</title>
<body>
<h1>It is SALE time at Gap!</h1>
<h2>Time for back to school.</h2>
<p>We configured this in the recipe</p>
</body>
</html>
Exercise: Upload the cookbook
knife cookbook upload mysite
Exercise: Viewing attributes using knife
$ knife node show node1 -l | more
...
§ Attributes:
§ tags:
§ Default Attributes:
§ apache:
§ indexfile: index2.html
§ Override Attributes:
§ Automatic Attributes (Ohai Data):
§ block_device:
§ dm-0:
§ removable: 0
§ size: 31563776
§ ...
2
Setting Attributes in Roles and Environments
Attributes can also be set in Roles and
Environments
These use raw JSON or Ruby format
default_attributes({"apache" => {"port" => 80}})
No comments:
Post a Comment