Search code examples
chef-infracookbook

Chef - Test for cookbook_file existence


Is there any way to test that a given cookbook_file exists in a chef recipe? I would like to do something like the following:

cookbook_file "/etc/cron.d/#{service}" do
  source "etc/cron.d/#{service}"
  owner "root"
  group "root"
  mode "0644"
end

and have it not fail if the file "etc/cron.d/{service_name}" does not exist, since I may have many services running on a machine, but only some of them have associated cron jobs.

I don't want to have a big list of services that take cron jobs like

['service_1', 'service_2', ...],

since that seems fairly brittle. Ideally, I'd like to have a directory that contains cron jobs for the services that need them, and have the recipe not fail if these files are not present. Is what I'm looking for possible?


Solution

  • As far as I know, it is not possible to test in advantage if the cookbook_file exists. But you can make chef continue the run instead of failing, if it didn't find the file.

    cookbook_file "/etc/cron.d/#{service}" do
      source "etc/cron.d/#{service}"
      owner "root"
      group "root"
      mode "0644"
      ignore_failure true
    end