I am trying to understand the Chef documentation on Custom Handlers.
The steps seem easy enough but I still don't understand how it works.
- Download the chef_handler cookbook
- Create a custom handler
- Write a recipe using the chef_handler resource
- Add that recipe to a node’s run-list, often as the first recipe in that run-list
Step 1 is no longer necessary as chef_handler cookbook is now part of Chef.
Step 2 creates a handler. The example given is
require 'net/smtp'
module OrgName
class SendEmail < Chef::Handler
def report
if run_status.failed? then
message = "From: sender_name <[email protected]>\n"
message << "To: recipient_address <[email protected]>\n"
message << "Subject: chef-client Run Failed\n"
message << "Date: #{Time.now.rfc2822}\n\n"
message << "Chef run failed on #{node.name}\n"
message << "#{run_status.formatted_exception}\n"
message << Array(backtrace).join('\n')
Net::SMTP.start('your.smtp.server', 25) do |smtp|
smtp.send_message message, 'sender@example', 'recipient@example'
end
end
end
end
end
Now comes step 3 which I don't understand, add the following to a recipe
send_email 'blah' do
# recipe code
end
When I run my recipe it just produces the error message which I expected to begin with:
FATAL: NoMethodError: undefined method `send_email` for cookbook: test, recipe: default :Chef::Recipe
How is this supposed to work? Are there other simple but working examples of custom handlers?
For Chef 15.0.300