简体   繁体   中英

How do I install dependencies for a chef handler?

I am trying to install a chef handler via the chef_handler lwrp. This handler (chef-handler-email) comes bundled in a gem. I am trying to install the gem then turn on the handler from within a single recipe that looks like:

chef_gem "chef-handler-mail"

chef_handler "MailHandler" do
  source 'chef/handler/mail'
  arguments :to_address => "root"
  action :nothing
  supports :exception => true, :report => false
end.run_action(:enable)

This works fine if the gem is already installed. However, if the Gem is not already installed I receive this error:

[2012-12-09T20:47:56-05:00] FATAL: LoadError: chef_handler[MailHandler] (chef_handler::email line 13) had an error: LoadError: no such file to load -- chef/handler/mail.rb

It appears as though the chef_handler resource is trying to load the handler before chef_gem has executed and installed the gem for the handler. I can obviously do this in a two step manual process where I have a separate recipe for installing the gem, then flip over to another recipe that configures the handler, but I'm hoping to avoid multi-step manual processes. Can it be done via single recipe?

I have a similar recipe for chef minitest-chef-handler:

chef_gem 'minitest'
chef_gem 'minitest-chef-handler'

require 'rubygems'
require 'minitest-chef-handler'

[... some unrelated code ...]

chef_handler "MiniTest::Chef::Handler" do
    source "minitest-chef-handler"
    arguments :verbose => true
    action :nothing
end.run_action( :enable )

Try requiring your gem before creating chef_handler resource, or may be source should be different...

The #run_action call causes the chef_handler resource to be run immediately at "compile" phase while the chef_gem resource is run during the "execute" phase as normally.

So also the gem needs to be installed at compile phase. And it seems that a require statement is also needed (as suggested in another answer) for Chef to load the gem.

chef_gem 'chef-handler-mail' do
  action :nothing
end.run_action(:install)

require 'chef/handler/mail'

chef_handler 'MailHandler' do
  source 'chef/handler/mail'
  # ... other attributes
  action :nothing
end.run_action(:enable)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM