[英]How to use a chef helper library from another chef cookbook in a ruby_block
AWS Opsworks: Chef version 11.10, Berkshelf version 3.2.0. AWS Opsworks:Chef版本11.10,Berkshelf版本3.2.0。
I can't figure out how to use a helper library from cookbook A in a ruby_block
in cookbook B. 我无法弄清楚如何在cookbook B的
ruby_block
中使用cookbook A中的辅助库。
I found a post discussing how to include a method in a ruby_block and another discussing how to share libraries across cookbooks but I can't get both working at the same time. 我找到一篇文章讨论如何在ruby_block中包含一个方法, 另一篇讨论如何在cookbook中共享库,但我不能同时使用它们。
cookbookA/libraries/helpers.rb cookbookA /库/ helpers.rb
module libraryA
module Helpers
def log(output)
Chef::Log.info("#{cookbook_name}:#{recipe_name}: #output}")
end
end
end
cookbookB/metadata.rb cookbookB / metadata.rb
depends 'cookbookA'
The following setup.rb works. 以下setup.rb有效。
cookbookB/recipes/setup.rb cookbookB /食谱/的setup.rb
::Chef::Recipe.send(:include, libraryA::Helpers)
log("this is a log")
However, when I use the log function in a ruby block, it fails. 但是,当我在ruby块中使用log函数时,它会失败。 The following setup.rb does not work:
以下setup.rb不起作用:
cookbookB/recipes/setup.rb cookbookB /食谱/的setup.rb
::Chef::Recipe.send(:include, libraryA::Helpers)
ruby_block "logging-function" do
block do
log("this is a log")
end
end
PS: I have also tried using ::Chef::Resource.send(:include, libraryA::Helpers)
PS:我也尝试过使用
::Chef::Resource.send(:include, libraryA::Helpers)
Updated code block: 更新的代码块:
::Chef::Recipe.send(:include, libraryA::Helpers)
ruby_block "logging-test" do
block do
::Chef::Recipe.send(:include, libraryA::Helpers)
::libraryA::Helpers.ttlog("message")
end
end
Received error: NoMethodError - undefined method ttlog for libraryA::Helpers:Module 收到错误:NoMethodError - libraryA :: Helpers:Module的未定义方法ttlog
Updated helpers 更新了帮助者
cookbookA/libraries/helpers.rb cookbookA /库/ helpers.rb
def log(output)
Chef::Log.info("#{cookbook_name}:#{recipe_name}: #output}")
end
PS: Removed the modules structure PS:删除了模块结构
I think ruby_block runs in other context, under other object. 我认为ruby_block在其他上下文中运行,在其他对象下运行。 You can try to include that library into provider, but I can't guarantee it would work.
您可以尝试将该库包含在提供程序中,但我不能保证它会起作用。
::Chef::Provider::RubyBlock.send(:include, libraryA::Helpers)
ps. PS。 Resource does have definition and list of parameters, but code is executed from Provider.
资源确实有参数的定义和列表,但代码是从Provider执行的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.