9

With 3200+ cookbooks available in Chef supermarket, often there is already a cookbook that covers the need I need to solve.

Almost every time there is some change required to a template, or a variable, or a resource in an existing cookbook.

What are the steps to override and/or improve existing cookbooks? Without forking their code, and retaining the ability to get version updates in the future.

1
  • 1
    Most cookbooks have a certain number of attributes which can simply be set while building a wrapper cookbook. You usually don't have to "override" anything to make expected tweaks.
    – chicks
    Commented May 9, 2017 at 12:06

1 Answer 1

13

There are a lot of specifics but the overall pattern we use is "wrap and extend". The general idea is to make a cookbook that depends on the community cookbook, usually named mycompany_originalthing, and then make recipes in that which call include_recipe 'originalthing::whatever' but with either more stuff added before/after or with calls to things like edit_resource to change resources. Avoid edit_resource when possible since it leads to brittle code, but it is there if you need it. You can also use wrapper cookbooks to set attributes, subclass or wrap custom resources, and so on.

For the specific case of "I need to tweak a template in a community recipe" it would look like this:

include_recipe 'original::whatever'

edit_resource!(:template, '/path/to/something') do
  source 'mytemplate.erb'
  cookbook 'mycompany_original'
end

You can find more details about edit_resource and friends at https://coderanger.net/rewind/

With cookbooks based around custom resources instead of recipes, things can get more complex but the specifics depend a lot on the how exactly the cookbook you are extending is written.

6
  • This is the pattern we follow and it usually works until tweaking the community cookbook has gotten too hard and it's easier to just rewrite it from scratch. Commented Mar 2, 2017 at 2:54
  • Great! Didn't even know they changed the old buggy rewind thing to something that is part of Chef. Commented Mar 2, 2017 at 4:21
  • Sorry to ask, but your username is also included in the link you mentioned ... Which makes me wonder if you have any affiliation with that link. If so, are you familiar with SE policies about includiing an appropriate "disclosure" about that (to be included in your answer also)?
    – Pierre.Vriens
    Commented Mar 5, 2017 at 7:57
  • 1
    @Pierre.Vriens in the context of the Chef world, my blog is roughly equivalent to the official docs. Check my history on SO if you want confirmation :)
    – coderanger
    Commented Mar 5, 2017 at 8:20
  • (in this case I don't link to the official docs because they contain syntax errors for this API)
    – coderanger
    Commented Mar 5, 2017 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.