Designing a cookbook for template overrides


#1

I am developing a library cookbook that configures some clustering software. All of the configuration is driven out of a text file that can be quite large and include lots of parameters that do not apply to most situations, thus I’m looking to have the cookbook user ultimately override this template via their own wrapper cookbook. I’d prefer that this override not require that the user fuss with the template resource (c.f ), rather use attributes to point the cookbooks to the wrapper template.

I’ve been able to get this working: I implement the template in the library cookbook thus:

template "config.txt" do
        path '/var/tmp/config.txt'
        source node['package']['template']
        cookbook node['package']['wrapper']
        variables({
            :variable => node["package"]["text"]
        })
end

I then create a cookbook “wrapper” with these attributes defined:

node.default['package']['wrapper'] = "wrapper"
node.default['package']['template'] = "wrapper-config.txt.erb"

create an appropriate template, and have a simple “include_recipe” in the wrapper’s default recipe to bring in the wrapped cookbook.

Now this all seems to work… however, being rather new to Chef I’m not certain if this is the most robust/reliable way to manage these overrides. If anyone can critique this approach or point to some other sources that allow this sort of function, I’d be grateful.

Thanks much

Michael


#2

Hello

The key to the approach is to be able to set the cookbook name and source which you are allowing with attributes which can be overridden.

An alternative approach is to pull out the resource from the resource queue using the resources method or to use Chef Rewind.

Here is an a good article by Seth Vargo


#3

Huh, looks like your original reply got b0rk3d somehow- anyway, you’d suggested using the resource tree:

resources("template[<your template resource name>]").cookbook "the wrapper cookbook"

or rewind to override the template in the library cookbook. Rewind looks very handy (I can see using that for a couple other problems I’ve got), but I’m kind of the opinion that for this cookbook- given that I’d like the template override to be an integral function of the cookbook- this needs to be built in somehow. Though I’m likely over-thinking the works… documenting these approaches is likely sufficient for the eventual user of this cookbook.

Thanks for looking at this


#4

Hi bugjunction.

Replied to three of four posts this morning during my lunch break and it doesn’t look like any saved correctly. Glad you somehow managed to get the original message.

I’ve updated my post with a link to a blog that will hopefully help others in a similar situation although your original solution does work, the approach does seem fair.

Chris.


#5

We have been writing library cookbooks for a lot of different clustered software and have found custom resources to work well for us. Our rather unique situation requires a lot of our internal (and some external) software to run on big iron UNIX (Solaris, AIX) as well as up to six flavor/versions of Linux. We have had extreme success with Policyfiles, especially in regards to easily overriding via attributes.

If you are interested, some examples: