I've written a chef recipe for SUSE, to install a package, wherein it's installing it however it keeps running the enable command every time when the chef-client runs, even though there is no change in the configuration.
我已经编写了一个用于SUSE的chef食谱,来安装一个包,其中安装了一个包,但是每次当chefl -client运行时,它都会继续运行enable命令,即使配置中没有变化。
recipe sample example:
配方样品的例子:
zypper_package 'apache2' do
action :install
end
service 'apache2' do
action [:enable, :start]
end
Please advise, how to make it idempotent?
请问如何使它具有幂等性?
2 个解决方案
#1
2
I've run into this situation with SuSE. At least with the version of chef i'm using (12.13, not sure about newer versions) seem to get the init determination wrong so the internal test to see if the service is already enabled does not work right.
我在SuSE上遇到了这种情况。至少在我使用的chef版本(12.13,不确定更新的版本)中,似乎错误地确定了初始化,因此检查服务是否已启用的内部测试无法正常工作。
The most interesting thing about it was that the provider i found to work on SLES 11 was Chef::Provider::Service::Redhat
. I had to handle supporting both 11 and 12 so i ended up using an if block within the resource definition
最有趣的是,我发现在SLES 11上工作的提供者是Chef::: provider::Service::Redhat。我必须同时支持11和12,所以最后我在资源定义中使用了if块
service 'apache2' do
action [:enable, :start]
if node['init_package'] != 'systemd'
provider Chef::Provider::Service::Redhat
end
end
I just spent the better part of 30 minutes digging through all of my cookbooks trying to find the original, but couldn't, i did just confirm that SLES SP4 exhibits the behavior you described, and that the above fixed it
我只是花了30分钟的时间翻遍了我所有的烹饪书,试图找到原作,但是我没能,我只是确认SLES SP4展示了你描述的行为,上面的方法解决了这个问题
-----> Converging <default-sles-11-sp4>...
Recipe: apache_test::default
* zypper_package[apache2] action install (up to date)
* service[apache2] action enable (up to date)
* service[apache2] action start (up to date)
#2
1
Try using not_if. You can wrap the enable in some kind of condition based on the presence of a configuration file to not run the command.
试着用not_if。您可以根据配置文件的存在而将启用条件封装到不运行该命令的情况下。
e.g.
如。
package 'Install Apache' do
package_name 'apache2'
action :enable
not_if do ::File.exists?(/some/config/file.conf) end
end
#1
2
I've run into this situation with SuSE. At least with the version of chef i'm using (12.13, not sure about newer versions) seem to get the init determination wrong so the internal test to see if the service is already enabled does not work right.
我在SuSE上遇到了这种情况。至少在我使用的chef版本(12.13,不确定更新的版本)中,似乎错误地确定了初始化,因此检查服务是否已启用的内部测试无法正常工作。
The most interesting thing about it was that the provider i found to work on SLES 11 was Chef::Provider::Service::Redhat
. I had to handle supporting both 11 and 12 so i ended up using an if block within the resource definition
最有趣的是,我发现在SLES 11上工作的提供者是Chef::: provider::Service::Redhat。我必须同时支持11和12,所以最后我在资源定义中使用了if块
service 'apache2' do
action [:enable, :start]
if node['init_package'] != 'systemd'
provider Chef::Provider::Service::Redhat
end
end
I just spent the better part of 30 minutes digging through all of my cookbooks trying to find the original, but couldn't, i did just confirm that SLES SP4 exhibits the behavior you described, and that the above fixed it
我只是花了30分钟的时间翻遍了我所有的烹饪书,试图找到原作,但是我没能,我只是确认SLES SP4展示了你描述的行为,上面的方法解决了这个问题
-----> Converging <default-sles-11-sp4>...
Recipe: apache_test::default
* zypper_package[apache2] action install (up to date)
* service[apache2] action enable (up to date)
* service[apache2] action start (up to date)
#2
1
Try using not_if. You can wrap the enable in some kind of condition based on the presence of a configuration file to not run the command.
试着用not_if。您可以根据配置文件的存在而将启用条件封装到不运行该命令的情况下。
e.g.
如。
package 'Install Apache' do
package_name 'apache2'
action :enable
not_if do ::File.exists?(/some/config/file.conf) end
end