Chaining resources?


#1

I have a situation that I (a newbie) don’t quite know how to handle. I have a situation where if one resource is run, I then want to stop a service, delete some files, and then start the service.

What’s the right way to do this? Do I create three additional resources (a service resource, a file resource, and another service resource) in my recipe and use notifications to kick off the new resources? Is there a way to encapsulate these three resources in a single block in my recipe? Do I break this sequence out into a separate recipe?

I’m sure there’s a simple solution to this problem; I’m just at a point in my Chef learning where I don’t know what that answer is yet.


#2

Hi @nogginblink,

Notifications are probably well suited for this person. You can set your first resource with a notification such that if it is updated, the notification will fire and cause other resources to run a particular action.

E.g.

my_resource 'foo' do
  action :create
  notifies :delete, 'file[/tmp/foo]', :delayed
  notifies :stop, 'service[foo]', :delayed
end

file '/tmp/foo' do
  action :nothing
end

service 'foo' do
  action :nothing
end

This is just hypothetical/example code, but yes, notifications. Cheers!