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?
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
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) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With