How do I share code across Chef cookbooks in a chef-repo?

谁说胖子不能爱 提交于 2019-12-04 19:03:41

问题


I would like to share a small handful of methods across recipes in a chef repo. I know that on a cookbook level I can put code in modules in the libraries directory (see related question). What I'm looking for is something like that but available across all of the cookbooks in my Chef repo.

I can think of a couple solutions:

  • Create a gem, install the gem as part of the chef run. This seems like overkill.
  • Put the file in some folder and add that folder to the $LOAD_PATH in the recipe file. I have a feeling that won't work with actual deployment because the chef server doesn't know anything about the repo.
  • Put the file in some folder and symlink that into the libraries directory of each cookbook.

The last option seems like the most viable. Is there a better/more idiomatic way to do what I want?


回答1:


You can use a library defined function from another cookbook but you must teach Chef that your cookbook depends on the providing cookbook.

So, for example, if in cookbook A, you have a libraries/default.rb that provides some function f, you can access it from cookbook B so long as B's metadata.rb file includes the line:

depends "A"

See the Chef documentation on metadata and libraries for more details.




回答2:


There are 3 distinct options allowing for sharing code in form of either chef resource (1. LWRP, 2. HWRP) or methods (3. "libraries"). I'd suggest you consider LWRPs first. I find this answer very good in explaining differences between mentioned techniques.



来源:https://stackoverflow.com/questions/17333486/how-do-i-share-code-across-chef-cookbooks-in-a-chef-repo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!