Priority Attributes in a Cook's Cookbook

I'm trying to implement a cookbook for wrappers, taking inspiration from How to Write a Cook's Reusable Cookbooks, Gangnam Style . I want to install tomcat 7 on my node without a manager application. I 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 specified 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 want to override these values โ€‹โ€‹for all attributes. However, attributes such as ["tomcat"]["user"] do not succeed. The above value has a tomcat6 value in node["tomcat"]["user"] .

Do I need to override all attributes related to ["tomcat"]["base_version"]}" ? If my attributes/default.rb was loaded before the tomcat default.rb cookbook, this would work well.

I use Berkshelf, Vagrant and Chef solo for development. In metadata.rb my cookbook, I mentioned depends "tomcat" .

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

+6
source share
2 answers

This is due to how / when the ruby โ€‹โ€‹code is evaluated during the Chef run. In a typical Chef run, attribute files are first processed in the dependency order given by run_list, as mentioned here: Chef 11 In-Depth: Attribute changes .

The chef discovers a dependency on the tomcat cookbook and first loads / evaluates its attributes. Therefore, default["tomcat"]["user"] = "tomcat#{node["tomcat"]["base_version"]} set to tomcat6 because at this time the value of node["tomcat"]["base_version"] equal to 6.

Later, the chef evaluates your cookbook wrapper and correctly sets the node["tomcat"]["base_version"] to 7, however node["tomcat"]["user"] never overestimated.

Therefore, you will need to set the value for node["tomcat"]["user"] in your wrapper cookie if you want to change its value.

+9
source

I ran into this problem. I also need to set the base variable and set other variables from it, for example:

 default["apache"]["apache_docroot"] = '/var/www' #other attributes: default['apache']['webapp1_docroot'] = "#{node['apache']['apache_docroot']/webapp1}" 

to get: /var/www/webapp1

As indicated, what happens is that the Chef will find your cookbooks and download their attributes first. Something seems to be wrong. Why not load the parent

 override["apache"]["apache_docroot"] = '/net1/websites' 

first, then the dependency will work fine. They are lower and will not be overridden.

I found a way around this problem. This is not great, but it works. You end up doing:

  • Depends on tomcat * .rb download
  • Download parent shell * .rb
  • Reboot depends on tomcat specific.rb

You can use this node.from_file command to reload attributes from another file:

 puts "*** RUNNING bundle-apache-java-tomcat-example default.rb" # Reload bundle-apache-java-jboss::default attributes to reset var depending on apache_docroot value node.from_file(run_context.resolve_attribute( "bundle-apache-java-tomcat", "default" ) ) 

this is a cookbook: bundle-apache-java-tomcat, attribs file: default.rb download cookbook: bundle-apache-java-tomcat, attribs file: default.rb

side of the note: I ended up leaving it in my cookbook, but I wanted to set the site attributes in a โ€œsimplerโ€ way using a hash. I cannot set one attribute from another while I initialize the hash at the same time, but I left this code there in case it might still be needed.

I have created two new cookbooks that I hope make it easy to set up multiple websites. You can also set or not set the proxy link from apache to tomcat.

https://github.com/stant/bundle-apache-java-tomcat-example (how to use the main) https://github.com/stant/bundle-apache-java-tomcat (main)

+1
source

All Articles