本文介绍了厨师库或定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Chef来说相对较新,我需要从现有食谱创建库或定义。

Being relatively new to Chef, I am required to create libraries or definitions from existing recipes.

那里的食谱使用bash资源,ruby块资源(这会通知另一个ruby)块资源(具有延迟时间的块资源),再次使用模板资源通知红宝石块等。

There recipes use bash resource, ruby block resource (which notifies another ruby block resource with delayed timing), template resource again which notifies a ruby block etc.

什么是最佳方法?库或定义?

我已阅读到如果使用定义,我将无法通知定义中的资源,这是否意味着我可以在其他定义文件中通知资源?

I have read that if I use definition, I won't be able to notify a resource within the definition, does that mean I can notify a resource in a different definition file?

我还读到在库中您不能直接使用资源。如果是这样,我该如何使用库中的资源?

I also read that in libraries you cant use the resources directly. If this is true, how can I use a resource within my library?

推荐答案

因此,这是主要基于意见,但我还是会回答。这里有4个不同的选择:

So, this is "primarily opinion based", but I'll answer it anyway. There are 4 distinct choices here:


  1. 定义

  2. LWRP

  3. HWRP


  1. Definition
  2. LWRP
  3. HWRP
  4. "Library"

定义只是一个或多个对象的包装带有一些参数化的更多资源。但是,定义没有添加到资源集合中。表示您无法通知或触发定义事件。它们仅用于包装和命名配方中发现的一系列可重复步骤。

A definition is just a wrapper around one or more resources with some parameterization. However, definitions are not added to the resource collection. Meaning you can't "notify" or trigger events on a definition. They are solely for wrapping and naming a series of repeatable steps found in a recipe.

LWRP(轻量级资源和提供程序)是特定于Chef的DSL,实际上在运行时编译为HWRP(重量级资源和提供程序)。 LWRP和HWRP都是Chef 扩展名。除了包装一系列可重复的任务外,* WRP还将在Chef中创建一个顶级资源(例如 template package )也可以在您的食谱和其他食谱的食谱中使用。

An LWRP (Light-weight resource and provider) is a Chef-specific DSL that actually compiles into an HWRP (Heavy-weight resource and provider) at runtime. Both LWRPs and HWRPs are Chef extensions. In addition to wrapping a series of repeatable tasks, *WRPs will create a top-level resource in Chef (like template or package) that's available for use in your recipe and other cookbook's recipes as well.

LWRP和HWRP之间的区别实际上是Ruby。 HWRP使用成熟的Ruby类。如果您不是Ruby开发人员,那么他们可能会有些吓人。但是,您应该在编写和进行LWRP之前尝试一下。 LWRP使用特定于Chef的DSL来创建资源。最终,它们将编译为(大约)与重量级对应代码相同的代码。我将在最后链接一些参考。

The difference between and LWRP and HWRP is really the Ruby. HWRPs use full-blown Ruby classes. If you aren't a Ruby developer, they may be a bit intimidating. Nonetheless, you should give it a try before writing and LWRP. LWRPs use a Chef-specific DSL for creating resources. At the end of the day, they compile to (roughly) the same code as the Heavy-weight counterpart. I'll link some references at the end. You have access to Chef resources inside either implementation, as well as the run_context.

最后,库(注意引号)常常被误解和滥用。它们是Ruby代码,被评估为Ruby,因此它们几乎可以做任何事情。 HWRP实际上是一种库形式。有时人们将图书馆用作帮助者。他们将使用 best_ip_for aggregate_some_data 之类的方法创建一个帮助器模块,然后将该库混合(Rubyism)到其食谱或资源来烘干东西。在其他时候,可以使用库来破坏 Chef本身。 食谱就是一个很好的例子。 太。图书馆实际上是一个未定义的领域,因为它们是王国的钥匙。

Finally, "libraries" (notice the quotes) are often misunderstood and abused. They are Ruby code, evaluated as Ruby, so they can do pretty much anything. HWRPs are actually a form of a library. Sometimes people use libraries as "helpers". They will create a helper module with methods like best_ip_for or aggregate_some_data and then "mix" (Rubyism) that library into their recipes or resources to DRY things up. Other times, libraries can be use to "hack" Chef itself. The partial-search cookbook is a good example of this. Facebook talked about how they limited the number of attributes sent back to the server last year at ChefConf too. Libraries are really an undefined territory because they are the keys to the kingdom.

因此,尽管我实际上没有回答您的问题(因为它基于观点),但我希望我已为您提供了有关前进的最佳方向的足够信息。请记住,每个基础架构都是特殊的雪花,没有正确的答案;只有一个最佳答案。我建议与您的团队共享此信息,并权衡每种方法的利弊。您也可以尝试厨师的邮件列表,在该列表上,人们会给您很多意见。

So, while I haven't actually answered your question (because it's opinion-based), I hope I've given you enough information about the best direction moving forward. Keep in mind that every infrastructure is a special snowflake and there is no right answer; there's only a best answer. I'd suggest sharing this information with your team and weighing the pros and cons of each approach. You can also try the Chef mailing list, on which people will give you many opinions.

资源:





  • LWRPs
  • HWRPs
  • Libraries

这篇关于厨师库或定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 23:33