为什么厨师不逐行执行配方?

时间:2015-10-29 06:47:38

标签: ruby chef

问题是厨师首先尝试安装模板,然后才安装软件包。如果我评论模板块,厨师将安装sphinxsearch包。

但是如果没有评论模板块,则不会安装sphinxsearch包,并且Chef会因错误而失败

  

资源模板[/etc/sphinxsearch/sphinx.conf]配置为通过动作重新加载通知资源服务[sphinxsearch],但在资源集合中找不到服务[sphinxsearch]

为什么会这样?

##
# Install system packages
##
node['website']['packages'].each do |pkg|
    log 'Installing ' + pkg
    package pkg
end

##
# Configure sphinx
##
template "/etc/sphinxsearch/sphinx.conf" do
    source 'sphinx.erb'
    owner 'root'
    group 'root'
    mode 00644
    notifies :reload, 'service[sphinxsearch]', :delayed
end

2 个答案:

答案 0 :(得分:4)

厨师中的

notifiessubscribes都试图接触厨师运行中定义的资源。然后,他们将对这些资源发出指示行动。在你的情况下:

notifies :reload, 'service[sphinxsearch]', :delayed

正在寻找名为service的{​​{1}}类资源,并在其上调用sphinxsearch操作。如果在资源收集(编译)阶段结束时,Chef无法找到reload资源,则会抛出错误。您没有看到安装包,因为Chef从未进入执行阶段。 (有关厨师两阶段性质的更多信息,请参阅this answer

如@IsabelHM所示,您可以通过添加

来解决问题
service[sphinxsearch]

我建议您使用service 'sphinxsearch' do action [:enable, :start] end 而不是[:enable, :start],因为这样可以确保服务始终在运行,即使您的模板没有更改。另请注意,:nothing资源为您添加服务配置。因此,如果sphinxsearch包未添加服务配置,您还需要servicecookbook_filetemplate资源来创建服务配置。

答案 1 :(得分:1)

在食谱中添加此内容。

service 'sphinxsearch' do
  action :nothing
end