2

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.

recipe sample example:

zypper_package 'apache2' do
action :install
end

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

Please advise, how to make it idempotent?

Zac
  • 1,305
  • 3
  • 17
  • 28
vinu
  • 113
  • 1
  • 6

2 Answers2

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.

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

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

-----> 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)
JoshHetland
  • 1,273
  • 1
  • 12
  • 22
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.

e.g.

package 'Install Apache' do
   package_name 'apache2'
   action :enable
   not_if do ::File.exists?(/some/config/file.conf) end
end
Rome_Leader
  • 2,518
  • 9
  • 42
  • 73
  • not_if and only_if are options to make it idempotent. So I just wondering are all built in recipes not idempotent by itself..? Shall we have to make it idempotent by providing conditional executions..? Any built in recipes in chef that are idempotent..? – Leo Prince May 24 '18 at 09:19