厨师:是否可以定义使用模板的厨师资源,然后从另一本食谱中调用它? [英] Chef: is it possible to define a Chef resource which uses a template, and call it from another cookbook?

查看:73
本文介绍了厨师:是否可以定义使用模板的厨师资源,然后从另一本食谱中调用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Chef中的模板的资源,并且工作正常。食谱中的其他资源和食谱可以称为该资源。就我而言,我定义了一个Maven工件资源,在其中提供了一些Maven参数(仓库名称,工件ID等)后,您可以从Nexus Maven仓库下载文件。

I had a resource which used a template in Chef, and it worked fine. Other resources and recipes in the cookbook could call this resource. In my case, I defined a maven artifact resource, where given some maven params (repo name, artifact ID, etc), you could download a file from a Nexus maven repo.

但是,当我从定义之外的其他任何食谱调用资源时(当然,使用 metadata.rb 指定依赖项),都会收到错误消息

However, when I call the resource from any other cookbook other than that in which it is defined (with metadata.rb specifying the dependency, of course), I get an error

 Chef::Exceptions::FileNotFound
           ------------------------------
           template[/usr/local/nexus-download.bash] (/tmp/kitchen/cache/cookbooks/cookbook-1/resources/nexus_http_artifact.rb line 52) had an error: Chef::Exceptions::FileNotFound: Cookbook 'cookbook-2' (1.0.0) does not contain a file at any of these locations:
[...]

模板基本上是一个bash脚本,在检查了MD5的总和并与MD5的总和进行比较后,随后运行该脚本从Maven存储库(Nexus)通过网络下载工件。

The template was basically a bash script that was later run to download an artifact over the network from a maven repo (Nexus), after checking the MD5 sum and comparing to the current download using the Nexus HTTP API.

推荐答案

您可以通过模板资源的 cookbook 属性控制模板的来源。对于自定义资源,cookbook属性默认为使用该资源而不是声明该资源的调用食谱。您可以轻松地覆盖此内容,因为您知道所写食谱的名称。所以我们可以这样:

You can control the cookbook the template comes from via the cookbook property of the template resource. For a custom resource the cookbook property defaults to the calling cookbook where the resource is used, rather than where it is declared. You can override this easily because you know the name of the cookbook you are writing. So we can so something like this:

cookbooks / mytemplate / resources / foo.rb

provides :foo

property :template_source, String, default: "foo.erb"
property :template_cookbook, String, default: "mytemplate"

action :run do
  template "/tmp/foo.xyz" do
    source new_resource.template_source
    cookbook new_resource.template_cookbook
  end
end

cookbooks / mytemplate / templates / foo.erb:

THIS IS THE DEFAULT TEMPLATE

cookbooks / test / metadata.rb:

name "test"
version "0.0.1"

depends "mytemplate"

菜谱/测试/食谱/default.rb:

foo "whatever"

然后,调用者可以使用其自己的模板名称和位置以及 dependency inject来覆盖它。模板放入资源:

Callers can then override this like with their own template names and locations and "dependency inject" the template into the resource:

食谱/测试/食谱/default.rb:

foo "whatever" do
  template_cookbook cookbook_name
  template_source "bar.erb"
end

菜谱/测试/模板/bar.erb:

THIS IS THE OVERRIDDEN TEMPLATE

The cookbook_file 资源具有一个 cookbook 属性,该属性应具有相同的作用。

The cookbook_file resource has a cookbook property that should work identically.

您可以使用此模式在野外在sous-chef Tomcat食谱中,其中定义了属性此处,稍后在模板资源中使用,它呈现此处此处

You can this pattern "in the wild" in the sous-chef tomcat cookbook where the properties are defined here and later used in the template resource it renders here and here

这篇关于厨师:是否可以定义使用模板的厨师资源,然后从另一本食谱中调用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆