Wrapper Cookbook Attribute Overwrite Question


#1

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.


#2

Have you considered using Chef Environments instead?

Something along the following lines.

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

description 'Development Environment'

default_attributes(
  'operations' => {
    'tomcat_instances' => {
      :dev => {
        :port => '80',
        :sites => %w[Dev]
      }
    }
  }
)
#
# 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/

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 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.


#3

On Thu, 12 Jun 2014 11:15:02 -0500
"David A. Williams" 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

Public Key = http://www.onddo.com/xabier_zuazo.pub
Key Fingerprint = 8EFA 5B17 7275 5F1F 42B2 26B4 8E18 8B67 9DE1 9468


#4

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 wrote:

On Thu, 12 Jun 2014 11:15:02 -0500
"David A. Williams" 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

Public Key = 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.