Wednesday, April 25, 2012

Chef : Notify resource from ruby_block

Lets say for some sane/insane reason , you want to notify a resource to take some action from within ruby_block , you can accomplish it as follows


ruby_block "dummy" do
    block do
        if some_condition_is_true
           resources(:file => "create_file").run_action(:create)
        end
    end
end

file "create_file" do
         path "/tmp/test.txt"
         owner "you"
         content "dummy"
         action :nothing
end

Caveat :

       If file resource "create_file" further notifies another resource , that resource will not be notified. What i mean to say is that calling a resource from within ruby_block by using run_action method directly does not build up the notification chain . For example if file resource was as below


file "create_file" do
         path "/tmp/test.txt"
         owner "you"
         content "dummy"
         action :nothing
         notifies :run , resources(:execute => "some_script")
end
and file resource is notified from within ruby_block , then file resource will not further notify "some_script" resource.

No comments:

Post a Comment