问题
Summary
Can I use an LWRP from an external cookbook without prefixing the cookbooks name? For example, if mycookbook
contains the mylwrp
resource how can I do this?
include_recipe 'mycookbook' # Contains "mylwrp"
mylwrp "frobulator" do
frobulations 42
end
Details
I have written an LWRP that gets the name mycookbook_mylwrp
since the library cookbook uses the normal resources/provider layout as follows
mycookbook/
├── providers
│ └── mylwrp.rb
└── resources
└── mylwrp.rb
Would it be possible to
- Keep the LWRP in the
mycookbook
cookbook - Use the LWRP without prefixing the
mycookbook
name (mycookbook_mylwrp
)
I realise that I can create a whole new cookbook and call it "mylwrp" and use the default providers as follows
mylwrp/
├── providers
│ └── default.rb
└── resources
└── default.rb
but then I have to make dedicated cookbooks for each LWRP I want to use in this manner.
回答1:
One option is to use HWRPs. These go in the libraries
directory rather than resources
and providers
. If you go that route, I'd look into the poise
library which allows you to write HWRPs with the same finesse as LWRPs.
Now, all that said, you are doing something that is decidedly outside the norm. The community is used to custom providers being namespaced to match the cookbook that declares them. If you want to share this cookbook with the community, you're going to cause some confusion. Even if you only use it internally, it will steepen the learning curve for any new members you bring onto your team. So unless you have a compelling reason to do it, I'd strongly suggest you avoid it.
On the other hand, if you do have a compeling reason, I'd suggest you share it so we can all benefit from your thoughts.
回答2:
No, LWRPs are all global and so are namespaced by the cookbook to prevent collisions. You also don't need the include_recipe, that is only involved in running recipe code. You do need the depends
line in the metadata.rb though.
来源:https://stackoverflow.com/questions/26457519/cookbook-name-prefix-when-calling-lwrp