Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 397
Marc Avatar asked Sep 24 '12 19:09

Marc


2 Answers

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
like image 108
Draco Ater Avatar answered Oct 03 '22 18:10

Draco Ater


Give this chef_gem a try: http://www.rubydoc.info/github/3ofcoins/chef-helpers

You can then use the has_source to only_if the file is in the recipe For example:

only_if { has_source?("optional.config.file", :files) }

like image 43
JonathanM Avatar answered Oct 03 '22 19:10

JonathanM