[英]How to use a chef helper library from another chef cookbook in a ruby_block
AWS Opsworks:Chef版本11.10,Berkshelf版本3.2.0。
我无法弄清楚如何在cookbook B的ruby_block
中使用cookbook A中的辅助库。
我找到一篇文章讨论如何在ruby_block中包含一个方法, 另一篇讨论如何在cookbook中共享库,但我不能同时使用它们。
cookbookA /库/ helpers.rb
module libraryA
module Helpers
def log(output)
Chef::Log.info("#{cookbook_name}:#{recipe_name}: #output}")
end
end
end
cookbookB / metadata.rb
depends 'cookbookA'
以下setup.rb有效。
cookbookB /食谱/的setup.rb
::Chef::Recipe.send(:include, libraryA::Helpers)
log("this is a log")
但是,当我在ruby块中使用log函数时,它会失败。 以下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:我也尝试过使用::Chef::Resource.send(:include, libraryA::Helpers)
更新的代码块:
::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
收到错误:NoMethodError - libraryA :: Helpers:Module的未定义方法ttlog
更新了帮助者
cookbookA /库/ helpers.rb
def log(output)
Chef::Log.info("#{cookbook_name}:#{recipe_name}: #output}")
end
PS:删除了模块结构
我认为ruby_block在其他上下文中运行,在其他对象下运行。 您可以尝试将该库包含在提供程序中,但我不能保证它会起作用。
::Chef::Provider::RubyBlock.send(:include, libraryA::Helpers)
PS。 资源确实有参数的定义和列表,但代码是从Provider执行的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.