为什么厨师不逐行执行食谱?

Why chef does not execute recipe line by line?

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

但是如果模板块没有注释,sphinxsearch 包没有安装,chef 失败并报错

resource template[/etc/sphinxsearch/sphinx.conf] is configured to notify resource service[sphinxsearch] with action reload, but service[sphinxsearch] cannot be found in the resource collection`

为什么会这样?

##
# 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

将此添加到您的食谱中。

service 'sphinxsearch' do
  action :nothing
end

notifies 和 Chef 中的 subscribes 都在尝试访问已在您的 Chef 运行 中定义的资源。然后他们将对这些资源调用指定的操作。在你的情况下:

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

正在寻找名为 sphinxsearchservice 类型的资源,并对其调用 reload 操作。如果在资源收集(编译)阶段结束时,chef 找不到 service[sphinxsearch] 资源,则会抛出错误。您看不到已安装的包,因为 chef 永远不会进入执行阶段。 (有关厨师的两阶段性质的更多信息,请参阅 this answer

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

来解决问题
service 'sphinxsearch' do
  action [:enable, :start]
end

我建议您使用 [:enable, :start] 而不是 :nothing,因为这将确保服务始终 运行ning,即使您的模板没有更改。另外,请注意 service 资源 不会 为您添加服务配置。因此,如果 sphinxsearch 包没有添加服务配置,您还需要 cookbook_filetemplateremote_file 资源来创建服务配置。