Search code examples
chef-infrachef-recipecookbook

How do I let a wrapper cookbook use its parent's template in a definition?


I'm working with the apache2 vendor cookbook, and my own app-cookbook. In my recipe, I want to use the web_app definition for a site and have it use the template defined in the vendor cookbook

#my-apache2/recipes/my-site.rb

web_app "my-site" do
    #template not specified here, so it should use default
    server_name "my-site.com"
    docroot "#{app_dir}/public"
end

Which results in

Error executing action `create` on resource 'template[/etc/httpd/sites-available/my-site.conf]'
================================================================================

Chef::Exceptions::FileNotFound
------------------------------
Cookbook 'my-apache2' (1.0.0) does not contain a file at any of these locations:

because it is looking in the my-site cookbook, not the apache2 templates directory.

I tried what worked on other resources:

resources("template[web_app.conf.erb]").cookbook "my-apache2"

but this one's path is dynamically generated based on a resource parameter (name).

#apache2/definitions/web_app.rb

application_name = params[:name] 
template "#{node['apache']['dir']}/sites-available/#{application_name}.conf" do
    source   params[:template]
...

So it returns this returns the error

Chef::Exceptions::ResourceNotFound
----------------------------------
Cannot find a resource matching template[web_app.conf.erb] (did you define it first?)

Is there a way to get it to use the vendor cookbook's template without copying it to my wrapper cookbook?


Solution

  • Looking at the source of the definition: https://github.com/onehealth-cookbooks/apache2/blob/45f08b4060f23573dbec65be32bf25baee56b734/definitions/web_app.rb#L35

    If you provide a cookbook parameter to the definition, then it will look for the template file in whatever cookbook you specify. (In this case it sounds like you want to specify apache2)

    web_app "my-site" do
      server_name "my-site.com"
      docroot "#{app_dir}/public"
      cookbook 'apache2'
    end