Attributes in CHEF
Prerequisite: https://bansalkushagra.medium.com/how-to-create-a-cookbook-and-recipe-in-chef-ae62e9ba8156
Types of Attributes (Priority Based):
A. Default
B. Force-default
C. normal
d. override
E. force-override
f. automatic
The attribute is key-value pair that represents a specific detail about a node. It is used to find the state of the node was at the end of the previous chef-client run. Or, what state of node should be at the end of the current chef-client run. Attributes are defined by Node, Roles, cookbook, recipe, and Environment.
Implementation:
Step-1: Access your Amazon EC-2 machine and follow the below command:
Command:
Ø sudo su: to go to the root
Ø ohai
Ø ohai ipaddress
Ø ohai memory/total
Ø ohai cpu/o/mhz
Step-2: Finding attributes using cookbook — recipe
Command:
Ø cd cookbooks
Ø cd apache-cookbook
Ø chef generate recipe recipe3
Ø cd .. // now we are inside the cookbooks directory
Ø vi apache-cookbook/ recipes/recipe3.rb
// .rb extension is mandatory
Code:
file ‘/basicinfo’ do
content “This is to get Attributes
HOSTNAME: #{node[‘hostname’]}
IPADDRESS: #{node[‘ipaddress’]}
CPU: #{node[‘cpu’][‘0’][‘mhz’]}
MEMORY: #{node[‘memory’][‘total’]}”
owner ‘root’
group ‘root’
action :create
end
Now, Run the recipe:
Ø chef-client -zr “recipe[apache-cookbook::recipe3]” // execute the recipe