Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Chef Tutorials: Working with Chef Attributes

Attributes represent information about your node. In addition to the information that can be automatically generated by ohai, you can set attributes in Chef recipes or in separate attribute files.

Because attributes can be defined in multiple places, all attribute values are composed together during a Chef run according to the priority levels

Attributes defined by ohai have the highest priority, followed by attributes defined in a recipe, then attributes defined in an attribute file. In other words, recipe attributes have a higher priority than those defined in attribute file in cookbooks, and will override them by default. Attributes defined by ohai trump everything else.


Declaring Attributes in Chef Recipe


Attributes can be set directly in recipes. Image shows the format of an attribute when it is set in a recipe. You must precede the attribute name with node. when you set an attribute directly in a recipe.


node.default['devopsschool']['message'] = 'Hello People'


log "Welcome to Chef, #{node['devopsschool']['message']}!" do
  level :info
end

log "Welcome to DevOps" do
  level :info
end

node.default['web']['file'] = '/opt/index.html'



file "#{node['web']['file']}" do
  content "Welcome to Chef Class"
end


Declaring Attributes in Chef Cookbooks


Attribute files are located in the attributes folder of a cookbook. Similar to recipes, the default attribute file is called:


In cookbook, I visualize three levels of attributes:

– Default values to converge successfully — attributes/default.rb

– Local testing override values — JSON or .kitchen.yml (have you tried chef_zero using ChefDK and Kitchen?)

– Environment/role override values

SUMMARY of Attributes in Recipe and Cookbook


In an attributes file you don't have to type 'node' so that this in attributes/default.rb:

default['foo']['bar']['baz'] = 'qux'

In an recipes/whatever.rb file

node.default['foo']['bar']['baz'] = 'qux'
Rajesh Kumar
Follow me
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x