Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOpsSchool!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started 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
endCode language: PHP (php)

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



file "#{node['web']['file']}" do
  content "Welcome to Chef Class"
endCode language: PHP (php)


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'Code language: PHP (php)
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x