gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:26:34 25 4
gpt4 key购买 nike

在一本 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com