avasin
avasin

Reputation: 9736

Why chef does not execute recipe line by line?

Problem is that chef tries to install template first, and only then installs packages. If i comment template block, chef will install sphinxsearch package fine.

But if template block is not commented, sphinxsearch package is not installed, and chef fails with error

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`

Why this happens?

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

Upvotes: 1

Views: 131

Answers (2)

Tejay Cardon
Tejay Cardon

Reputation: 4223

notifies and subscribes in chef are both trying to reach out to resources that have been defined in your chef run. They will then call teh indicated action on those resources. In your case:

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

is looking for a resource of type service named sphinxsearch and call the reload action on it. If, at the end of the resource gathering (compile) phase, chef cannot find a service[sphinxsearch] resource, then it throws the error. You don't see the package installed because chef never enters the execution phase. (See this answer for more on the two phase nature of chef)

As indicated by @IsabelHM, you could solve the problem by adding

service 'sphinxsearch' do
  action [:enable, :start]
end

I suggest you use [:enable, :start] rather than :nothing as this will ensure that the service is always running, even if your template doesn't change. Also, please note that the service resource does not add a service config for you. So if the sphinxsearch package does not add a service config, you'll also need a cookbook_file, template, or remote_file resource to create the service config with.

Upvotes: 4

display name
display name

Reputation: 4185

Add this in your recipe.

service 'sphinxsearch' do
  action :nothing
end

Upvotes: 1

Related Questions