Streamline Your Infrastructure Management Using Chef Attributes

If you are managing infrastructure with Chef, you are already familiar with the concept of cookbooks. But did you know that you can customize them to meet your specific needs using attributes? 

Attributes are a powerful feature of Chef that allows for flexible and customizable infrastructure management.  

They are used to make Chef cookbooks configurable, allowing you to override default values set in cookbooks with your own. This makes it easy to enforce organization rules or customize cookbooks for specific nodes or environments. 

For example, consider a cookbook that has hard-coded values or installation steps that are not compatible with your organization's rules. You can either patch or rewrite it. However, both options require significant manual effort. This is where attributes contribute a much simpler and efficient solution by enabling one to customize and configure cookbooks without having to hard code the values. By superimposing values that fit better with your personal environment over the default values, the daunting task of customizing cookbooks is now a breeze. 

The Chef Infra client uses attributes to understand: 

  • The current state of the node 
  • What the state of the node was at the end of the previous Chef Infra Client run
  • What the state of the node should be at the end of the current Chef Infra Client run 

Setting up your Environment  

Regardless of whether you are working on a Windows, macOS or Linux workstation, the best way to get started is to install Chef Workstation, a collection of tools that enable you to create, test and run Chef code.  

Attributes can be defined in the following ways: 

  • Node saved on the Chef Infra server
  • Passed via JSON on command line
  • Policyfiles
  • Cookbooks 
In this blog, we will understand how attributes can be defined via cookbooks. 

You can refer to the Demo of how one can install apache in two unique machines using the same or follow the steps mentioned below. 

Let us consider two nodes,  

  1. One running Ubuntu 20.04 
  2. The other, Centos 7 
The installation of Apache on these two nodes will be quite different from each other as the ubuntu environment requires the installation of the apache2 package whereas the centos environment uses the httpd package. 

Without the use of attributes, one can create individual cookbooks and recipes or use specific code in a single recipe to install the packages in the nodes. 

The more efficient approach however, would be to use the attributes file within the cookbook and override a simple recipe which would allow the installation of the packages across any environment.  

1. Create an attribute file package.rb  within your cookbook using the following command. 

chef generate attributes package 

This creates a file package.rb in the directory /chef-repo/cookbooks/attributes/ 

This file will let us deploy a simple recipe that will install Apache for the appropriate environments. 

2. In the package.rb file, add the following code snippet which defines which package should be installed based on the node. A simple if-else like code can be used to install the httpd package if the node is of centos environment or the apache2 package otherwise. 

case node[‘platform’]
when ‘centos’
    node.default[‘apache’][‘package’] = ‘httpd’
    node.default[‘apache’][‘package’] = ‘apache2’

3. In the recipe webserver.rb, which will enable the installation process in the first place, use the following code snippet. 

package node[‘apache’][‘package’] do
     action :install
service node[‘apache’][‘package’] do
     action [:enable, :restart]

4. With the files updated, use the knife command to upload these modified files to the Chef Infra server. Replace <path> with the path to your cookbooks. 

$ knife cookbook upload apache –cookbook-path /<path> 

5. Run the commands to check if the proper cookbook is listed in the run list of each node.

$ knife node show web-ubuntu 
$ knife node show web-centos7 

6. Go back to your systems and run the chef client. The installation would have been initiated in both nodes. 

$ sudo chef-client 

7. Upon completion, run the command to check if apache has been installed in each of the nodes.


$ systemctl status apache2 


$ systemctl status httpd 

Attributes are always applied by the chef-client in the following order:

  1. The default attribute specified in a cookbook attribute file. 
  2. The default attribute specified in a recipe. 
  3. The default attribute specified in an environment. 
  4. The default attribute specified in a role.  
More details on Attribute types, priorities here.   

To sum it up, attributes diminish the manual labour required to customize your infrastructure management by eliminating the need for extensive manual patching or rewriting of cookbooks. This flexibility enables you to adapt your infrastructure to meet specific needs and ensure compatibility with your organization's guidelines. Say goodbye to tedious manual rewrites and embrace the efficiency of attribute-based cookbook management. 

Now that you have a solid understanding of the power of attributes in Chef, you may want to delve deeper into the topic: 




Sarthak Vasal

Sarthak Vasal is a Technical Product Intern at Progress Chef. With a background in computer science, Sarthak brings a blend of technical expertise and business acumen to the table. As part of the Technical Product Marketing team, he is driven by a deep fascination for the journey from an idea to a fully realized product and its market success.