Chef conditionals


#1

Problem: I have a chef statement that should only run if the attribute is “true”. But it runs every time

Expected Behavior: When default[:collectd][:service][:restart]== “true”, service should restart and if I override the attribute from chef-role to false then it should not restart

Actual Behavior: No matter what the attribute is set to, it always skips

Attribute file:
default[:collectd][:service][:restart]== “true”

Recipe file:
service “collectd” do
only_if { node[‘collectd’][‘service’][‘restart’] == ‘true’}
supports :status => true, :restart => true, :reload => true
action [ :enable, :start ]
end

Overridding attributes in chef-role:

“collectd”: {
“service”: {
“restart”: “false”
}
}

chef-client logs with and without override attribute (shows same):
Recipe: collectd_middleware_parameter::_service

  • service[collectd] action enable (skipped due to only_if)
  • service[collectd] action start (skipped due to only_if)

#2

Remove one of the =, you’re never setting the attribute in attribute file here, just doing a logical statement (which probably return false, but that’s stored nowhere.


#3

silly mistake by me!

It worked! Thanks and catch you soon :slight_smile: