Specifying cookbook name in template resource

When creating a custom resource and including a template resource, should I include the “cookbook” property and should this be a relative reference or the static name of the cookbook? E.g.

Which is the best way to specify the cookbook name (I’ve seen different patterns in various cookbooks)?

template ‘/path’ do
cookbook ‘microfocus’

or

template ‘/path’ do
cookbook cookbook_name

or

template ‘/path’ do
cookbook new_resource.cookbook

or not at all?

Also, when including any template resource in a normal recipe, unless I specify either the cookbook property, or use File.join for the source property, I get foodcritic warning FC033. Is this expected or is it a bug?

Thanks, Richard

Hi, does anyone have any pointers on this one please? I can’t find any documentation about it. Thanks, Richard

From

https://docs.chef.io/resource_template.html

cookbook

    Ruby Type: String

    The cookbook in which a file is located (if it is not located in the current cookbook). The default value is the current cookbook.

The key is if it is not located in the current cookbook.

I’m guessing that your custom resource is placed in a library cookbook which does not have the template file?

In that case, you might need to supply the cookbook, it depends on whether Chef will create the resources used in your custom resource under the current cookbook, i.e. the cookbook name set to where you consume your custom resource, I don’t think Chef will use the cookbook name that contains the definition of the custom resource. You can determine whether it is the former or latter by putting a pry breakpoint in your custom resource or output the resources cookbook to the log (similar to below)

t = template '/etc/mflmrcscript' do
  source '/etc/mflmrcscript.erb'
  mode 0755
  owner 'root'
  group 'root'
  variables(license_manager_path: license_manager_path)
end

puts t.cookbook
=begin
Or use log class
=end
Chef::Log.info("Cookbook #{t.cookbook}")
=begin
Or use log resource
=end
log ""Cookbook #{t.cookbook}"

If cookbook name is wrong then you’ll need to specify the cookbook name as an attribute in your custom resource, you could call it template_cookbook,

property :template_cookbook, kind_of: String

If you use an attribute called cookbook then it’ll conflict with the built in resource cookbook property and you will have to use new_resource.

The principal behind the old Lightweight Resource Providers (LWRP) was that the definition of the resource as described in the recipe file is passed in to your LWRP provider as new_resource. The provider is the actual implementation of your resource actions, in the provider you then load the current state of the resource, compare the new resource against the current state of the resource, i.e. the desired state (new resource) and current state, the provider then works towards changing the resource into the desired state (as defined by new_resource). Ordinarily you need to do this with custom resources unless the property name

Additional info, Chef stores files/templates etc in the cookbook collection manifests and does a lookup into the manifest to retrieve the relevant files, hence cookbook is needed.

We’ve written quite a few cookbooks that wrap community cookbooks and use our own template files rather than the one’s supplied by the community, we can do this very easily in a recipe file thanks to the resources method …

=begin
 In recipe file ...Use our own template for xxxx
=end
resources("template[#{node['apache']['dir']}/apache.conf]").cookbook "our_cookbook"

Hope these pointers help, first thing to do is check to see what the value of cookbook in your custom resources template.

Thanks Chris, that’s helpful. In the custom resource that I’m writing, the template is in the same cookbook as the resource definition. This resource will then be consumed in wrapper cookbooks as follows:

microfocus_server_express ‘/opt/microfocus/cobol’ do
checksum ''
license_number ''
serial_number ''
url ''
end

Presumably, if I add the “cookbook ‘microfocus’” property to the template resource in the custom resource definition in the ‘microfocus’ cookbook, I then won’t need to add it when creating microfocus_server_express resources in wrapper cookbooks? I just wasn’t sure if it was actually required as the template resource only gets used internally within the custom resource. It doesn’t need to be called elsewhere.

Thanks, Richard