patternModerate
What steps could be taken to inherit and override some parts of a Chef cookbook?
Viewed 0 times
cookbookchefwhatpartscouldoverridetakensomeandinherit
Problem
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.
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.
Solution
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
For the specific case of "I need to tweak a template in a community recipe" it would look like this:
You can find more details about
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.
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'
endYou 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.
Code Snippets
include_recipe 'original::whatever'
edit_resource!(:template, '/path/to/something') do
source 'mytemplate.erb'
cookbook 'mycompany_original'
endContext
StackExchange DevOps Q#195, answer score: 13
Revisions (0)
No revisions yet.