问题
Cookbook A provides a LWRP that I would like to extend with a LWRP or HWRP in cookbook B, so that I could do something like the following, where provider_b
would use the existing code/resources in provider_a
and accept additional attributes it could use for it's own actions:
provider_a "A" do
attr_from_a :value
end
provider_b "B" do
attr_from_a :value
attr_from_b :value
end
Is this possible, and is it still possible if I want to avoid editing cookbook A?
回答1:
It sounds like you're trying to create a sub-resource of an existing LWRP, so you're not "wrapping" it - you're "extending it". The LWRP syntax makes this less than desirable, because the resources are dynamically compiled into Ruby classes at runtime.
You could switch to HWRPs (the new Jenkins cookbook is a good example that uses inheritance and OO to extend resources and share attributes). By their nature, LWRPs are not very extensible, since they are dynamically rebuilt and reloaded at runtime.
回答2:
I think the section about Custom LWRPs in the documentation and the tutorial linked at the end should help you.
EDIT: Okay, maybe looking at this LWRP or this LWRP goes a bit more into the right direction (as I think that's a very common pattern for LWRPs, I didn't go so much into detail). You can access the parameters using new_resource.param_a
.
So something like this should work:
action :install do
provider_a "A" do
param_a new_resource.param_a
end
provider_b "B" do
param_a new_resource.param_a
param_b new_resource.param_b
end
end
来源:https://stackoverflow.com/questions/21143396/how-can-i-write-a-chef-provider-that-extends-an-existing-provider