1.

What are libraries in Chef? When we use them?

Answer»

Libraries store arbitrary Ruby code that could be reused ACROSS the recipes within any cookbook that depends on the library. This is often located in the library folder of our cookbooks. First and the foremost usage of libraries are to provide helper methods that reduce the code duplication and also provide a mechanism to hide the implementation logic in the recipes. Libraries get loaded first when the Chef LOADS our cookbooks.It make use of  “ load_libraries_from_cookbook” method from the “Chef::RunContext::CookbookCompiler” class. Typical use cases of libraries are 

  • For creating modules containing methods used in our recipes. Functions are defined inside a module which is in-turn included in our recipes as a mixin. This is done by including module namespace in our recipe using “include” keyword and hence MAKING all methods defined in the module AVAILABLE to recipes as if declared locally.
  • Libraries extend Chef’s core classes and even helps in OVERRIDING their methods. For eg: if we need to modify a method in Chef:: Node class, you can define the method in a library.
  • Facilitates the creation and implementation of heavyweight resources and providers.


Discussion

No Comment Found