How to use the assistant chef library from another cookbook in ruby_block

AWS Opsworks: chef version 11.10, version Berkshelf 3.2.0.

I can't figure out how to use the helper library from cookbook A in ruby_block in cookbook B.

I found a post discussing how to include a method in ruby_block and also discussing how to share cookbook libraries, but I can't get both to work at the same time.

cookbookA / libraries / 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' 

The following setup.rb works.

cookbookB / Recipes / setup.rb

  ::Chef::Recipe.send(:include, libraryA::Helpers) log("this is a log") 

However, when I use the log function in the ruby ​​block, it fails. The following setup.rb does not work:

cookbookB / Recipes / setup.rb

  ::Chef::Recipe.send(:include, libraryA::Helpers) ruby_block "logging-function" do block do log("this is a log") end end 

PS: I also tried using ::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 ttlog method for library A :: Helpers: Module

Updated Helpers

cookbookA / libraries / helpers.rb

 def log(output) Chef::Log.info("#{cookbook_name}:#{recipe_name}: #output}") end 

PS: Removed module structure

+4
source share
1 answer

I think ruby_block works in a different context under a different object. You can try to include this library in the provider, but I can not guarantee that this will work.

::Chef::Provider::RubyBlock.send(:include, libraryA::Helpers)

ps. The resource has a definition and a list of parameters, but the code is executed from the provider.

+1
source

All Articles