Bis*_*Das 2 cookbook chef-infra chef-recipe chef-solo lwrp
在食谱中,我有一个库(client_helper.rb)。在其中定义了一个模块。模块名称为Client_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)
Run Code Online (Sandbox Code Playgroud)
现在,我有默认食谱。我在直接配方中调用方法network_zone的地方正在工作。
但是,当我在ruby_block(例如Client_helper.network_zone)内调用方法network_zone时,它不起作用。
请找到配方代码。
# 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
Run Code Online (Sandbox Code Playgroud)
我的食谱目录结构-
请帮我。
无需将方法注入任何提供程序类,最好仅将其注入所需的类中:
Chef::Recipe.send(:include, Client_helper)
Chef::Resource::RubyBlock.send(:include, Client_helper)
Run Code Online (Sandbox Code Playgroud)
通过注入方法,您可以对这些类进行猴子补缀,而这带来了与“ monkeypatching”相关的所有风险(谷歌搜索可能具有教育意义)。
如果将#network_zone帮助程序注入Chef :: Provider和Chef :: Resource基类,这些基类将覆盖任何核心资源或提供程序或任何菜谱资源或提供程序中任何类似命名的方法。如果其他人使用该名称的方法,则会破坏他们的代码。