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'
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND