Pergunta

I have a cookbook for installing a base product (Mediaflux), and a second cookbook that tailors it for a particular specialised use-case (DaRIS). The first cookbook's recipe is run to do the "base install" and the second one's recipe is run to "tailor" the installation.

At one point, I have a file created by the base cookbook/recipe that needs extra stuff to be added in the second cookbook/recipe. In both cases, template expansion is required for this file.

I'm trying to figure out a good way to implement this.

  • The simplest idea is to have a copy of the template in both cookbooks, and have both recipes expand their version template, and rely on the 2nd recipe to overwriting the file created by the first one.

    • This is not DRY, as I have two copies of the template.

    • I'm not sure that this is "kosher" ... having one template expansion clobber another.

  • A variation would be to try to have only one template expansion (in the base recipe) and parameterize the cookbook name ... so that adding the second recipe to the runlist causes the first one to use a different version of the template.

    • This is not DRY either.

    • There is the issue that the base recipe doesn't know about the tailoring recipe's expansion parameters for the template. (Maybe the template can get them directly from the node attributes ...)

Ideally, I'd like to be able to do one of the following:

  • Create a template that combines two (DRY) templates when it is expanded.

  • Expand the second (tailoring) template in a way that appends it to the file created by the first recipe.

  • Get the "template" resource to expand the two templates to one file in a single operation.

Is there any way to do any of these things?

Is there another approach that I've missed?

Foi útil?

Solução

The chef documentation describes the optional "cookbook" attribute that enables you to specify from where the template should be retrieved:

template "/path/to/this/file" do
  source "file.erb"
  cookbook "myothercookbook"
  mode 0440
  owner "me"
  group "me"
  variables({
     :var1 => node[:mycurrentcookbook][:var1],
     :var2 => node[:mycurrentcookbook][:var2]
  })
end

This enables common templates to be re-used.

Outras dicas

To partly answer my own Question, one template can incorporate another using the Ruby "render" method. This is documented here: http://docs.opscode.com/resource_template.html#partial-templates

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top