# Wrapper Cookbook Attribute Overwrite Question

**URL:** <https://discourse.chef.io/t/wrapper-cookbook-attribute-overwrite-question/5404>\
**Category:** Chef Infra (archive)\
**Created:** [June 12, 2014, 4:15pm UTC](https://discourse.chef.io/t/wrapper-cookbook-attribute-overwrite-question/5404 "2014-06-12T16:15:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_A\_Williams](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/david_a_williams/32/209_2.png) [@David\_A\_Williams](https://discourse.chef.io/u/David_A_Williams)\
**Post date:** [June 12, 2014, 4:15pm UTC](https://discourse.chef.io/t/wrapper-cookbook-attribute-overwrite-question/5404/1 "2014-06-12T16:15:02Z")

</div>

Hello,

Chef 11.2.4

I’m setting up a wrapper cookbook to overwrite some node attributes. However it appears when that attribute is a hash it runs both configs. Has anyone experienced this?

For example in my base cookbook, I have  
attributes/default.rb  
default[‘operations’][‘tomcat\_instances’] = {:Dev =\> { :port =\> ‘80’, :sites =\> %w[Dev] }}

And in my wrapper cookbook, I have  
recipes/raven\_server.rb  
node.set[‘operations’][‘tomcat\_instances’] = {:QA=\> { :port =\> ‘80’, :sites =\> %w[Master Staging] }}

After the chef run. I have two Tomcat instances set up instead of just one.

Thanks for your help,

David Williams

* * *

CONFIDENTIALITY NOTICE: This electronic mail may contain information that is privileged, confidential, and/or otherwise protected from disclosure to anyone other than its intended recipient(s). Any dissemination or use of this electronic mail or its contents by persons other than the intended recipient(s) is strictly prohibited. If you have received this communication in error, please notify the sender immediately by reply e-mail so that we may correct our internal records. Please then delete the original message. Thank you.

---

<div class="post-metadata">

**Author:** ![anon10447422](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@anon10447422](https://discourse.chef.io/u/anon10447422)\
**Post date:** [June 12, 2014, 10:20pm UTC](https://discourse.chef.io/t/wrapper-cookbook-attribute-overwrite-question/5404/2 "2014-06-12T22:20:43Z")

</div>

Have you considered using Chef Environments instead?

Something along the following lines.

```auto
#
# chef/environments/devel.rb
#
name 'devel'

description 'Development Environment'

default_attributes(
  'operations' => {
    'tomcat_instances' => {
      :dev => {
        :port => '80',
        :sites => %w[Dev]
      }
    }
  }
)

```

```auto
#
# chef/environments/qa.rb
#
name 'qa'

description 'QA Environment'

default_attributes(
  'operations' => {
    'tomcat_instances' => {
      :QA => {
        :port => '80',
        :sites => %w[Master Staging]
      }
    }
  }
)

```

I would urge you to take a look at the following link that tries to  
clarify the definitions of - library, application, wrapper and base  
cookbook patterns.

[http://blog.vialstudios.com/the-environment-cookbook-pattern/](http://blog.vialstudios.com/the-environment-cookbook-pattern/)

Chef attribute precedence and cookbook patterns are **tied at the hip** ,  
so please develop the intuition on how they are related.

Also **don't** confuse environment cookbook pattern to Chef  
environments. They are very different ideas unfortunately named with  
similar terminology.

The best way to think of environment cookbook is as an entry point into  
your Chef Run. It provides something equivalent to what `int main() { ... }` function does in a C program.

Best,  
Rajiv

On Thu, Jun 12, 2014 at 9:15 AM, David A. Williams [dwilliams@daxko.com](mailto:dwilliams@daxko.com) wrote:

> Hello,
> 
> Chef 11.2.4
> 
> I'm setting up a wrapper cookbook to overwrite some node attributes. However it appears when that attribute is a hash it runs both configs. Has anyone experienced this?
> 
> For example in my base cookbook, I have  
> attributes/default.rb  
> default['operations']['tomcat\_instances'] = {:Dev =\> { :port =\> '80', :sites =\> %w[Dev] }}
> 
> And in my wrapper cookbook, I have  
> recipes/raven\_server.rb  
> node.set['operations']['tomcat\_instances'] = {:QA=\> { :port =\> '80', :sites =\> %w[Master Staging] }}
> 
> After the chef run. I have two Tomcat instances set up instead of just one.
> 
> Thanks for your help,
> 
> David Williams
> 
> * * *
> 
> CONFIDENTIALITY NOTICE: This electronic mail may contain information that is privileged, confidential, and/or otherwise protected from disclosure to anyone other than its intended recipient(s). Any dissemination or use of this electronic mail or its contents by persons other than the intended recipient(s) is strictly prohibited. If you have received this communication in error, please notify the sender immediately by reply e-mail so that we may correct our internal records. Please then delete the original message. Thank you.

---

<div class="post-metadata">

**Author:** ![zuazo\_onddo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/zuazo_onddo/32/137_2.png) [@zuazo\_onddo](https://discourse.chef.io/u/zuazo_onddo)\
**Post date:** [June 12, 2014, 11:39pm UTC](https://discourse.chef.io/t/wrapper-cookbook-attribute-overwrite-question/5404/3 "2014-06-12T23:39:48Z")

</div>

On Thu, 12 Jun 2014 11:15:02 -0500  
"David A. Williams" [dwilliams@daxko.com](mailto:dwilliams@daxko.com) wrote:

> I'm setting up a wrapper cookbook to overwrite some node attributes.  
> However it appears when that attribute is a hash it runs both  
> configs. Has anyone experienced this?
> 
> For example in my base cookbook, I have  
> attributes/default.rb  
> default['operations']['tomcat\_instances'] = {:Dev =\> { :port =\>  
> '80', :sites =\> %w[Dev] }}
> 
> And in my wrapper cookbook, I have  
> recipes/raven\_server.rb  
> node.set['operations']['tomcat\_instances'] = {:QA=\> { :port =\>  
> '80', :sites =\> %w[Master Staging] }}
> 
> After the chef run. I have two Tomcat instances set up instead of  
> just one.

That's because you are setting different attribute types (default vs.  
normal,set). In this case the Hashes are merged instead of replaced.

You should use same attribute type like for example:

node.default['operations']['tomcat\_instances'] = {  
:Dev =\> { :port =\> '80', :sites =\> %w[Dev] }  
}

# ...

node.default['operations']['tomcat\_instances'] = {  
:QA=\> { :port =\> '80', :sites =\> %w[Master Staging] }  
}

Or reset default attribute type:

node.default['operations']['tomcat\_instances'] = {  
:Dev =\> { :port =\> '80', :sites =\> %w[Dev] }  
}

# ...

node.default['operations']['tomcat\_instances'] = {}  
node.set['operations']['tomcat\_instances'] = {  
:QA=\> { :port =\> '80', :sites =\> %w[Master Staging] }  
}

I know this can be confusing, but keep in mind that the attributes are  
of type Hash internally (Mash). These two things have the same effect:

node.set['operations']['tomcat\_instances'] = {  
:QA=\> { :port =\> '80', :sites =\> %w[Master Staging] }  
}

node.set['operations']['tomcat\_instances']['QA']['port'] = '80'  
node.set['operations']['tomcat\_instances']['QA']['sites'] = %w[  
Master  
Staging  
]

## -- Xabier de Zuazo Oteiza IT System Administrator - Onddo Labs S.L. [www.onddo.com](http://www.onddo.com)

## Public Key = [http://www.onddo.com/xabier\_zuazo.pub](http://www.onddo.com/xabier_zuazo.pub) Key Fingerprint = 8EFA 5B17 7275 5F1F 42B2 26B4 8E18 8B67 9DE1 9468

---

<div class="post-metadata">

**Author:** ![David\_A\_Williams](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/david_a_williams/32/209_2.png) [@David\_A\_Williams](https://discourse.chef.io/u/David_A_Williams)\
**Post date:** [June 13, 2014, 12:46pm UTC](https://discourse.chef.io/t/wrapper-cookbook-attribute-overwrite-question/5404/4 "2014-06-13T12:46:50Z")

</div>

Xabier and Rajiv,

Thank you for your responses! Xabier I went with your approach and it  
worked. Rajiv, I use environments for cookbook versions.

David Williams

On 6/12/14 6:39 PM, "Xabier de Zuazo" [xabier@onddo.com](mailto:xabier@onddo.com) wrote:

> On Thu, 12 Jun 2014 11:15:02 -0500  
> "David A. Williams" [dwilliams@daxko.com](mailto:dwilliams@daxko.com) wrote:
> 
> > I'm setting up a wrapper cookbook to overwrite some node attributes.  
> > However it appears when that attribute is a hash it runs both  
> > configs. Has anyone experienced this?
> > 
> > For example in my base cookbook, I have  
> > attributes/default.rb  
> > default['operations']['tomcat\_instances'] = {:Dev =\> { :port =\>  
> > '80', :sites =\> %w[Dev] }}
> > 
> > And in my wrapper cookbook, I have  
> > recipes/raven\_server.rb  
> > node.set['operations']['tomcat\_instances'] = {:QA=\> { :port =\>  
> > '80', :sites =\> %w[Master Staging] }}
> > 
> > After the chef run. I have two Tomcat instances set up instead of  
> > just one.
> 
> That's because you are setting different attribute types (default vs.  
> normal,set). In this case the Hashes are merged instead of replaced.
> 
> You should use same attribute type like for example:
> 
> node.default['operations']['tomcat\_instances'] = {  
> :Dev =\> { :port =\> '80', :sites =\> %w[Dev] }  
> }
> 
> # ...
> 
> node.default['operations']['tomcat\_instances'] = {  
> :QA=\> { :port =\> '80', :sites =\> %w[Master Staging] }  
> }
> 
> Or reset default attribute type:
> 
> node.default['operations']['tomcat\_instances'] = {  
> :Dev =\> { :port =\> '80', :sites =\> %w[Dev] }  
> }
> 
> # ...
> 
> node.default['operations']['tomcat\_instances'] = {}  
> node.set['operations']['tomcat\_instances'] = {  
> :QA=\> { :port =\> '80', :sites =\> %w[Master Staging] }  
> }
> 
> I know this can be confusing, but keep in mind that the attributes are  
> of type Hash internally (Mash). These two things have the same effect:
> 
> node.set['operations']['tomcat\_instances'] = {  
> :QA=\> { :port =\> '80', :sites =\> %w[Master Staging] }  
> }
> 
> node.set['operations']['tomcat\_instances']['QA']['port'] = '80'  
> node.set['operations']['tomcat\_instances']['QA']['sites'] = %w[  
> Master  
> Staging  
> ]
> 
> ## -- Xabier de Zuazo Oteiza IT System Administrator - Onddo Labs S.L. [www.onddo.com](http://www.onddo.com)
> 
> ## Public Key = [http://www.onddo.com/xabier\_zuazo.pub](http://www.onddo.com/xabier_zuazo.pub) Key Fingerprint = 8EFA 5B17 7275 5F1F 42B2 26B4 8E18 8B67 9DE1 9468

CONFIDENTIALITY NOTICE: This electronic mail may contain information that is privileged, confidential, and/or otherwise protected from disclosure to anyone other than its intended recipient(s). Any dissemination or use of this electronic mail or its contents by persons other than the intended recipient(s) is strictly prohibited. If you have received this communication in error, please notify the sender immediately by reply e-mail so that we may correct our internal records. Please then delete the original message. Thank you.
