I read about Chef wrapper cookbook and now I'm try to refactor some kitchen roles in role cookbooks. So for instance I have a lamp role cookbook which does something like this:
# This is coming from another cookbook
include_recipe 'mysql_role::default'
# Install and tune apache2
include_recipe 'lamp_role::apache2'
# Install php and usual modules
include_recipe 'lamp_role::php'
include_recipe 'lamp_role::php_modules'
Included recipes may have some application logic but they usually end with inclusion of official cookbooks
# in lamp_role::php recipe
include_recipe 'php'
Now in another recipe I'd like to check if the official php::default
recipe has been included somewhere, I used to do that with one of these two syntaxes:
# This is true for recipes included both in roles and in run_list
include_recipe 'newrelic::php_agent' if node.recipes.include?('php')
# This works only for runlist recipes
include_recipe 'newrelic::php_agent' if node.recipe?('php')
Is there any way to check if a recipe has been included with include_recipe
somewhere else?
P.S. I'm using chef-solo to deploy.
Use case
I wrote a monitoring recipe which installs monit and add some templates, here's a fragment of code
# Install and configure monit
include_recipe 'monit'
# Template shouldn't be installed if apache is not present
monitrc 'apache2' do
template_cookbook 'monitoring_role'
# My previous attempt, which doesn't work if recipe is included with include_recipe
only_if { node.recipes.include?('apache2') || node[:monit][:templates].include?('apache2') }
end
I've added an attribute (node[:monit][:templates]
) to manually specify monit templates in node attributes, but it's pretty easy to forget them. Obviously I don't want the apache2
recipe included in all hosts but I'm using this recipe in multiple machines and with the check I'm asking for I can reuse the recipe on all hosts and monit will be installed with templates for daemons present in the current node.