Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attribute precedence in chef cookbook

I am trying to implement a wrapper cookbook by taking inspiration from How to Write Reusable Chef Cookbooks, Gangnam Style. I wish to install tomcat 7 on my node without manager app. I have created a wrapper cookbook with the following attributes/default.rb file:

default["tomcat"]["base_version"] = 7
default["tomcat"]["deploy_manager_apps"] = false

The default attributes provided in tomcat/attributes/default.rb are:

default["tomcat"]["base_version"] = 6
#other attributes
default["tomcat"]["deploy_manager_apps"] = true
default["tomcat"]["user"] = "tomcat#{node["tomcat"]["base_version"]}

I wish to override these values across all attributes. However attributes such as ["tomcat"]["user"] are not getting overriden. The above still has the value of tomcat6 in node["tomcat"]["user"].

Do I have to override all the attributes which refer to ["tomcat"]["base_version"]}"? If my attributes/default.rb were loaded before tomcat cookbook's default.rb this would have worked fine.

I am using Berkshelf, Vagrant and Chef solo for development. In metadata.rb of my cookbook, I have mentioned depends "tomcat".

My custom cookbook is located at https://github.com/vaibhavguptaIITD/hcentive-ops/tree/master/hc-tomcat and tomcat community cookbook is located at https://github.com/opscode-cookbooks/tomcat.

like image 738
Vaibhav Avatar asked Jan 08 '14 15:01

Vaibhav


People also ask

How do you override attributes in Chef?

An override attribute is automatically reset at the start of every chef-client run and has a higher attribute precedence than default, force_default, and normal attributes. An override attribute is most often specified in a recipe, but can be specified in an attribute file, for a role, and/or for an environment.

What is node in Chef recipe?

A node is any machine—physical, virtual, cloud, network device, etc. —that is under management by Chef.

What is Chef-client?

A chef-client is an agent that runs locally on every node that is under management by Chef. When a chef-client is run, it will perform all of the steps that are required to bring the node into the expected state, including: Registering and authenticating the node with the Chef server.


1 Answers

This is due to how/when ruby code is evaluated during a Chef run. In a typical Chef run, the attribute files are evaluated first, in the dependency order dictated by the run_list as mentioned here: Chef 11 In-Depth: Attributes Changes.

Chef detects the dependency on the tomcat cookbook and loads/evaluates it's attributes first. So default["tomcat"]["user"] = "tomcat#{node["tomcat"]["base_version"]} is set to tomcat6 because at the time, the value of node["tomcat"]["base_version"] is 6.

Later, Chef evaluates your wrapper cookbook and properly sets the node["tomcat"]["base_version"] attribute to 7, however node["tomcat"]["user"] is never reevaluated.

So you will need to set the value for node["tomcat"]["user"] in your wrapper cookbook if you would like to change it's value.

like image 73
Michael Goetz Avatar answered Sep 26 '22 08:09

Michael Goetz