仅在特定属性更改时重新启动服务

时间:2015-07-24 18:01:34

标签: chef

假设您有2对属性和1个对应的模板,并且每对属性用于不同的服务。如何单独重新启动每个服务,而不是在两对属性中只有一个发生更改时重新启动两个服务。谢谢!

#recipe
template "/etc/security/limits.conf" do
  source 'limits.conf.erb'
  mode '0644'
  notifies :restart, 'service[nginx]' #need code to restart separately
  notifies :restart, 'service[memcached]' #same as above
end

#attributes
default['nginx']['www-data']['soft'] = 32000
default['nginx']['www-data']['hard'] = 32000
default['memcache']['soft'] = 32000
default['memcache']['hard'] = 32000

#template
www-data soft nofile <%= node['nginx']['www-data']['soft'] %>
www-data hard nofile <%= node['nginx']['www-data']['hard'] %>
memcache hard nofile <%= node['memcache']['hard'] %>
memcache soft nofile <%= node['memcache']['soft'] %>

1 个答案:

答案 0 :(得分:1)

我建议你尝试添加一个中间人ruby块来管理服务。您需要将下面的if和elsif语句替换为用于检查要启动哪个服务的语句。 -

template '/etc/security/limits.conf' do
  source 'limits.conf.erb'
  mode '0644'
  notifies :run, 'ruby_block[start_right_service]', :immediately
end

ruby_block 'start_right_service' do
  action :nothing
  block do
    if [# nginx attributes changed]
       self.notifies :restart,'service[nginx]',:immediately
    elsif [# memcachedattributes changed]
       self.notifies :restart,'service[memcached]',:immediately
    else
       self.notifies :restart,'service[nginx]',:immediately
       self.notifies :restart,'service[memcached]',:immediately
    end    
  end
end