[RESOLVED] Learning chef Attributes


#1

Hi All,

I am in process of learning chef attributes. I have written this recipe to test it:

node[“chef”][“site”].each do |sitename, data|
template “/home/ec2-user/#{sitename}.txt” do
source "template_test.erb"
mode "0755"
owner "root"
group "ec2-user"
variables(:site_name => sitename, :port => data[‘port’], :ssl => data[‘ssl’])
end
end

This cookbook is working as I expected. But what is that ‘node’ keyword signify?

Krish


#2

The node keyword signifies the Node object which is where you set attributes.

For example, with an attributes file:

# attributes/default.rb

default['chef']['site']['bears'] = { port => 8080, ssl => true }
default['chef']['site']['clowns'] = { port => 8081, ssl => false }

As the above is an attribute file, there is a special DSL that lets us use default, if you wanted to set this in a recipe then you’d use node.default. Anytime you use node.LEVEL it is setting a node attribute, when calling it by node['attr'] you are reading it. Attributes are just one big Hash (it’s technically a Mash but it acts just like a hash in most respects)

So in this example the each method expects node[‘chef’][‘site’] to be a Hash and then it iterates over that hash, using sitename, data to represent the key/value. For the first pass, sitename = bears and data = { port => 8080, ssl => true }.

More information can be found on the Attributes documentation and illustrative examples can be found at Learn Chef

-cheeseplus