在cookbook中创建动态资源

时间:2014-08-21 14:53:14

标签: ruby cookbook chef

长话短说,这是我的代码:

template "...." do

    ....

    notifies :restart,"service[myservice_One]"
    notifies :restart,"service[myservice_Two]"
end

['One', 'Two'].each do |proj|
    service 'myservice_#{proj}' do 
        start_command "setsid /etc/init.d/my_command #{proj} start"
        stop_command "setsid /etc/init.d/my_command #{proj} stop"
        restart_command "setsid /etc/init.d/my_command #{proj} restart"
        status_command "setsid /etc/init.d/my_command #{proj} status"
        supports :status => true, :restart => true
        action [:enable,:start]
    end
end

我收到了这个错误:

template[...] is configured to notify resource service[celery_worker_One] with action restart, but service[celery_worker_One] cannot be found in the resource collection.

我知道我可以复制我的代码以使其工作但是如何动态创建服务?

谢谢!

1 个答案:

答案 0 :(得分:2)

在Ruby中,

'myservice_#{proj}'

"myservice_#{proj}"

非常不同:

'myservice_#{proj}' #=> "myservice_#{proj}"
"myservice_#{proj}" #=> "myservice_One"

如果您需要interpolation,则必须使用双引号。这就是您无法通知资源的原因。