Search code examples
rubychef-infrasendmailchef-recipe

Subscribes/Notifies in Chef


I have recently started working on CHEF Recipes and was trying to learn about subscribes/notifies in CHEF

Scenario: Wrote a recipe to install sendmail package, have a local copy of sendmail.mc on CHEF that i am deploying on the Node[Client].

I read about notifies/subscribes and what i am trying to do is when there is a change in the sendmail.mc file at /etc/mail/sendmail.mc sendmail service should be cycled.

for which i am using subscribes . But when i manually change the sendmail.mc on the client to trigger Chef to overwrite the file and wait for the service to be restarted for sendmail i see the below error

  • service[sendmail] action nothing (skipped due to action :nothing)

package "sendmail"

service "sendmail" do
    action [:enable, :start] 
end 

cookbook_file '/etc/mail/sendmail.mc' do
    action [:create]
    source 'sendmail.mc'
    owner 'root'
    group 'root'
end

service 'sendmail' do
    subscribes :restart, 'file[/etc/mail/sendmail.mc]', :immediately
end


Solution

  • I think the duplicate service definition with the same name ("sendmail") could be a reason. But also your subscribes targets the wrong resource file instead of cookbook_file!

    Given that most users and public cookbooks use "notifies" instead of "subscribes", I would rewrite it to:

    package 'sendmail'
    
    cookbook_file '/etc/mail/sendmail.mc' do
      source 'sendmail.mc'
      owner 'root'
      group 'root'
      action :create
      notifies :restart, 'service[sendmail]', :immediately # not sure about the *:immediately* here, usually at the end of converge is sufficient.
    end
    
    service 'sendmail' do
      action [:enable, :start]
    end