Access chef resources inside ruby block

大兔子大兔子 提交于 2019-12-05 21:08:01

问题


I've been trying to find the answer to this in the chef docs and through Google, but I've not been able to come up with anything. I'm not a ruby guy (yet), so the answer to this might stem from my approaching the problem with "just enough ruby for Chef". Here's what I want to do: in my deploy resource, in the before_migrate attribute, I want to execute a resource in my current recipe. What I am doing currently is to just stuff the resource into the block itself, but I know there must be a better way to do it.

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 

What I'm aiming for is something like

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

So I can re-use that template code. Thanks!


回答1:


You could specify the resource outside of the "deploy" resource with an action of nothing and then, in the *before_migrate* do something like:

    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

That way, you can notify it when you need it.




回答2:


Thanks to the great guys in the #chef IRC channel, I solved my problem. The notification resource needs to be accessed directly, using

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

Which will notify the template resource to run the :create action.



来源:https://stackoverflow.com/questions/14946273/access-chef-resources-inside-ruby-block

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!