# Designing a cookbook for template overrides

**URL:** <https://discourse.chef.io/t/designing-a-cookbook-for-template-overrides/8541>\
**Category:** Chef Infra (archive)\
**Created:** [May 23, 2016, 9:23pm UTC](https://discourse.chef.io/t/designing-a-cookbook-for-template-overrides/8541 "2016-05-23T21:23:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bugjunction](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/bugjunction/32/1141_2.png) [@bugjunction](https://discourse.chef.io/u/bugjunction)\
**Post date:** [May 23, 2016, 9:23pm UTC](https://discourse.chef.io/t/designing-a-cookbook-for-template-overrides/8541/1 "2016-05-23T21:23:12Z")

</div>

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](https://www.chef.io/blog/2013/12/03/doing-wrapper-cookbooks-right/) ), 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

---

<div class="post-metadata">

**Author:** ![chris\_sullivan](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@chris\_sullivan](https://discourse.chef.io/u/chris_sullivan)\
**Post date:** [May 24, 2016, 7:34am UTC](https://discourse.chef.io/t/designing-a-cookbook-for-template-overrides/8541/2 "2016-05-24T07:34:32Z")

</div>

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

> **[Seth Vargo | Evangelism, Technology, Rants, and Pants](https://www.sethvargo.com/changing-chef-resources-at-runtime/)**
>
> As the cookbook wrapper pattern becomes more prevalent, the need to change resources in a wrapped cookbook becomes more apparent. This post describes how to search for a Chef resource and tweak its attributes from another recipe.

---

<div class="post-metadata">

**Author:** ![bugjunction](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/bugjunction/32/1141_2.png) [@bugjunction](https://discourse.chef.io/u/bugjunction)\
**Post date:** [May 24, 2016, 11:25am UTC](https://discourse.chef.io/t/designing-a-cookbook-for-template-overrides/8541/3 "2016-05-24T11:25:06Z")

</div>

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](https://github.com/thommay/chef-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

---

<div class="post-metadata">

**Author:** ![chris\_sullivan](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@chris\_sullivan](https://discourse.chef.io/u/chris_sullivan)\
**Post date:** [May 24, 2016, 11:53am UTC](https://discourse.chef.io/t/designing-a-cookbook-for-template-overrides/8541/4 "2016-05-24T11:53:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jbellone](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/jbellone/32/1179_2.png) [@jbellone](https://discourse.chef.io/u/jbellone)\
**Post date:** [May 26, 2016, 11:57am UTC](https://discourse.chef.io/t/designing-a-cookbook-for-template-overrides/8541/5 "2016-05-26T11:57:02Z")

</div>

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:

> **[bloomberg/collectd-cookbook](https://github.com/bloomberg/collectd-cookbook)**
>
> Contribute to collectd-cookbook development by creating an account on GitHub.

  

> **[bloomberg/redis-cookbook](https://github.com/bloomberg/redis-cookbook)**
>
> redis-cookbook - A set of Chef recipes for installing and configuring Redis.

  

> **[johnbellone/consul-cookbook](https://github.com/johnbellone/consul-cookbook)**
>
> consul-cookbook - Application cookbook which installs and configures Consul.
