访问ruby块内的厨师资源

我一直试图在厨师文档和谷歌中找到答案,但我无法想出任何东西。 我不是一个ruby家伙,所以答案可能源于我接近“厨师足够ruby”的问题。 这就是我想要做的事情:在我的deploy资源中,在before_migrate属性中,我想在我当前的配方中执行资源。 我目前正在做的是将资源填充到块本身,但我知道必须有更好的方法来实现它。

 before_migrate do template "#{app_root}/#{applet_name}/local_settings.py" do source "local_settings.py.erb" owner app_config['user'] group app_config['group'] variables( :database_name => app_config['postgresql']['database_name'], :user => app_config['postgresql']['user'], :password => app_config['postgresql']['password'] ) action :create end end 

我的目标是什么

 before_migrate do "template #{app_root}/#{applet_name}/local_settings.py".execute end 

所以我可以重复使用该模板代码。 谢谢!

您可以使用“无”操作指定“部署”资源之外的资源,然后在* before_migrate *中执行以下操作:

  before_migrate do ruby_block "notify_template" do block do true end action :create notifies :create, "template[#{app_root}/#{applet_name}/local_settings.py]", :immediately end end 

这样,您可以在需要时通知它。

感谢#chef IRC频道中的好人,我解决了我的问题。 需要使用直接访问通知资源

Chef::Resource::Notification.new("template[#{app_root}/#{applet_name}/local_settings.py", :create)

这将通知template资源以运行:create action。