Configuring sane defaults in Chef with default generator templates can help kickstart projects and deliver results more quickly.
on this page
Now that you’ve written some cookbooks after learning about some of the config management primitives and running through the standard “deploy a website with apache” walkthroughs you’re finding that you make some of the same customizations to every cookbook.
Instead of (sometimes, hopefully remembering to…wait what about that one thing?) manually making those same modifications for every cookbook, drop those configurations and overrides in a generator cookbook with chef generate generator base_origin.
Now every object that you would create with the chef generate command (app, cookbook, recipe, attribute, resource, etc.) references the generator recipe you wrote, including all the modifications and customizations that you added. And this doesn’t benefit just you alone. Generator cookbooks give your team a standardized set of configurations and expectations for every cookbook and recipe, letting everyone learn from each other’s past discoveries and mistakes.
First you have to generate the generator (replace base_origin with whatever you want your generator cookbook to be named).
chef generate generator base_origin
Next make whatever changes you want to your baseline generator. We’ll go over a few possible modifications shortly.
You have to reference the generator cookbook in your config.rb.
if File.basename($PROGRAM_NAME).eql?('chef') && ARGV[0].eql?('generate')
chefdk.generator.license= "all_rights"
chefdk.generator.copyright_holder = "<Yournamehere>"
chefdk.generator.email = "<yourname>@<yourdomain>.com"
chefdk.generator_cookbook = "/vagrant/configs/base_origin"
end
Before you start using your generator cookbook overrides in the chef development process, generate a few test cookbooks and inspect the results to verify the changes you made do exactly what you intended:
chef generate cookbook test_cookbook
Alright so what might make sense to override in a generator cookbook? Here are a few ideas to inspire.
Then apply any modifications you want to see in your generator to the files/default/spec_helper.rb file in the generator cookbook.
Then apply any modifications you want to see in your generator to the templates/default/recipe_spec.rb.erb file in the generator cookbook.
Then apply any modifications you want to see in your generator to the templates/default/kitchen.yml.erb file in the generator cookbook.
Generally speaking, if you find yourself making the same modifications to every cookbook or recipe that you write then that is a good candidate for a generator override.