chef-infra - 如何在 Recipe Recipe 中使用库模块

标签 chef-infra chef-recipe chef-solo cookbook lwrp

在一本 Recipe 中,我有一个图书馆( client_helper.rb )。在其中定义了一个模块。模块名称为 客户 helper .这是模块代码。

module Client_helper
# This module contains helper methods

def network_zone
        Chef::Log.debug('network zone called...********')
        Chef::Log.debug("inside-::::"+self.class.to_s)
end    

end
Chef::Recipe.send(:include, Client_helper)

现在我有 默认 Recipe 。我在哪里调用方法 network_zone 从直接配方来看,它正在起作用。

但是当我在 中调用方法 network_zone 时ruby_block(eg. Client_helper.network_zone) 它不工作 .

请找到 Recipe 代码。
# Cookbook: client
# Recipe: default

Chef::Resource.send(:include, Sap_splunk_client_helper)   


  host_network_zone = network_zone # This is working

Log.info("inside-::::"+self.class.to_s)

ruby_block 'parse auto generated templates' do
  block do
    host_network_zone = Client_helper.network_zone #This is not working
    Log.info("inside ruby block-::::"+self.class.to_s)
end
end

我的 Recipe 目录结构-

enter image description here

请帮我。

最佳答案

无需将方法注入(inject)任何提供程序类,最好只将其注入(inject)您需要的类:

Chef::Recipe.send(:include, Client_helper)
Chef::Resource::RubyBlock.send(:include, Client_helper)

通过注入(inject)方法,您正在对这些类进行修补,这会带来与“猴子修补”相关的所有风险(谷歌搜索可能具有教育意义)。

如果您将 #network_zone 帮助器注入(inject) Chef::Provider 和 Chef::Resource 基类,这将覆盖任何核心资源或提供者,或任何 Recipe 资源或提供者中任何类似命名的方法。如果其他人使用该名称的方法,您将破坏他们的代码。

关于chef-infra - 如何在 Recipe Recipe 中使用库模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37434589/

相关文章:

chef-infra - 如何在移动文件之前让 Chef 等待应用程序部署?

git - 有没有办法在 Berkshelf 中使用私有(private) git 存储库

ubuntu - 某环境下的 Knife 列表内核版本

linux - Chef-client 是否在启动时运行?

Vagrant Chef solo 无法访问 Recipe

chef-infra - 在不合并的情况下覆盖默认哈希

chef-infra - 需要自定义 LWRP

docker - Vagrant , docker , puppet , Chef

chef-infra - Chef中的执行顺序

chef-infra - Chef solo nginx Recipe 不符合我的属性