仅当特定属性更改时才重新启动服务
Restarting Service Only If Specific Attribute Changes
假设您有 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'] %>
我建议您尝试添加中间人 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
假设您有 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'] %>
我建议您尝试添加中间人 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