Reputation: 27986
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?
Upvotes: 5
Views: 1042
Reputation: 4176
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)
Upvotes: -1
Reputation: 21226
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...
Upvotes: 2