# Defining attributes for cookbook in recipe has different behavior than attributes from roles

**URL:** <https://discourse.chef.io/t/defining-attributes-for-cookbook-in-recipe-has-different-behavior-than-attributes-from-roles/6144>\
**Category:** Chef Infra (archive)\
**Created:** [December 4, 2014, 7:37am UTC](https://discourse.chef.io/t/defining-attributes-for-cookbook-in-recipe-has-different-behavior-than-attributes-from-roles/6144 "2014-12-04T07:37:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Jimenez](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/william_jimenez/32/181_2.png) [@William\_Jimenez](https://discourse.chef.io/u/William_Jimenez)\
**Post date:** [December 4, 2014, 7:37am UTC](https://discourse.chef.io/t/defining-attributes-for-cookbook-in-recipe-has-different-behavior-than-attributes-from-roles/6144/1 "2014-12-04T07:37:49Z")

</div>

Hello

1. I’m using the opscode chef-client cookbook, and I am trying to define  
attributes for the cookbook from a wrapper recipe, however they don’t seem  
to be honored. When I define them in a role the node is in, it works  
however (working meaning chef\_client operates on the attributes). What is  
different about the two?

Here is my attributes file:

exception\_mail\_template = “/opt/chef/mail\_hanlder\_template.erb”

default[‘chef-client’][‘config’][‘exception\_handlers’] = [  
{“class” =\> “MailHandler”, “arguments” =\> [{:template\_path =\>  
"#{exception\_mail\_template}"}]  
}]  
default[‘chef-client’][‘load\_gems’] = [  
{“chef-handler-mail” =\> {“require\_name” =\> “chef/handler/mail”}  
}]

and portion of my recipe where I call chef\_client

# Exception handling for chef client runs

chef\_gem “chef-handler-mail” do  
action :install  
end

# Install exception handler email template

exception\_mail\_template = “/opt/chef/mail\_hanlder\_template.erb”

cookbook\_file “#{exception\_mail\_template}” do  
source "mail\_handler.erb"  
end

include\_recipe “chef-client::config”

Thanks

---

<div class="post-metadata">

**Author:** ![Galen\_Emery](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/galen_emery/32/11_2.png) [@Galen\_Emery](https://discourse.chef.io/u/Galen_Emery)\
**Post date:** [December 4, 2014, 7:54pm UTC](https://discourse.chef.io/t/defining-attributes-for-cookbook-in-recipe-has-different-behavior-than-attributes-from-roles/6144/2 "2014-12-04T19:54:05Z")

</div>

On Wed, Dec 3, 2014 at 11:37 PM, William Jimenez \<  
[william.jimenez@itsoninc.com](mailto:william.jimenez@itsoninc.com)\> wrote:

> Hello
> 
> 1. I'm using the opscode chef-client cookbook, and I am trying to  
> define attributes for the cookbook from a wrapper recipe, however they  
> don't seem to be honored. When I define them in a role the node is in, it  
> works however (working meaning chef\_client operates on the attributes).  
> What is different about the two?
> 
> Here is my attributes file:
> 
> exception\_mail\_template = "/opt/chef/mail\_hanlder\_template.erb"
> 
> default['chef-client']['config']['exception\_handlers'] = [  
> {"class" =\> "MailHandler", "arguments" =\> [{:template\_path =\>  
> "#{exception\_mail\_template}"}]  
> }]  
> default['chef-client']['load\_gems'] = [  
> {"chef-handler-mail" =\> {"require\_name" =\> "chef/handler/mail"}  
> }]
> 
> and portion of my recipe where I call chef\_client
> 
> # Exception handling for chef client runs
> 
> chef\_gem "chef-handler-mail" do  
> action :install  
> end
> 
> # Install exception handler email template
> 
> exception\_mail\_template = "/opt/chef/mail\_hanlder\_template.erb"
> 
> cookbook\_file "#{exception\_mail\_template}" do  
> source "mail\_handler.erb"  
> end
> 
> include\_recipe "chef-client::config"
> 
> Thanks

Attribute Precedence is probably what's screwing you up:

> **[About Attributes](https://docs.chef.io/attributes/)**
>
> An attribute is a specific detail about a node. Attributes are used by Chef Infra Client to understand:
> The current state of the node What the state of the node was at the end of the previous Chef Infra Client run What the state of the node should be...

Your default attribute in your attributes file would have the same  
precedence as the cookbook. So order wins. A role will win though.

---

<div class="post-metadata">

**Author:** ![William\_Jimenez](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/william_jimenez/32/181_2.png) [@William\_Jimenez](https://discourse.chef.io/u/William_Jimenez)\
**Post date:** [December 4, 2014, 8:04pm UTC](https://discourse.chef.io/t/defining-attributes-for-cookbook-in-recipe-has-different-behavior-than-attributes-from-roles/6144/3 "2014-12-04T20:04:18Z")

</div>

Thanks Galen, I just tried changing the attributes file to

force\_override['chef-client']['config']['exception\_handlers'] = [  
{"class" =\> "MailHandler", "arguments" =\> [{:template\_path =\>  
"#{exception\_mail\_template}"}]  
}]  
force\_override['chef-client']['load\_gems'] = [  
{"chef-handler-mail" =\> {"require\_name" =\> "chef/handler/mail"}  
}]

No difference. What else could it be?

On Thu, Dec 4, 2014 at 11:54 AM, Galen Emery [galen@getchef.com](mailto:galen@getchef.com) wrote:

> On Wed, Dec 3, 2014 at 11:37 PM, William Jimenez \<  
> [william.jimenez@itsoninc.com](mailto:william.jimenez@itsoninc.com)\> wrote:
> 
> > Hello
> > 
> > 1. I'm using the opscode chef-client cookbook, and I am trying to  
> > define attributes for the cookbook from a wrapper recipe, however they  
> > don't seem to be honored. When I define them in a role the node is in, it  
> > works however (working meaning chef\_client operates on the attributes).  
> > What is different about the two?
> > 
> > Here is my attributes file:
> > 
> > exception\_mail\_template = "/opt/chef/mail\_hanlder\_template.erb"
> > 
> > default['chef-client']['config']['exception\_handlers'] = [  
> > {"class" =\> "MailHandler", "arguments" =\> [{:template\_path =\>  
> > "#{exception\_mail\_template}"}]  
> > }]  
> > default['chef-client']['load\_gems'] = [  
> > {"chef-handler-mail" =\> {"require\_name" =\> "chef/handler/mail"}  
> > }]
> > 
> > and portion of my recipe where I call chef\_client
> > 
> > # Exception handling for chef client runs
> > 
> > chef\_gem "chef-handler-mail" do  
> > action :install  
> > end
> > 
> > # Install exception handler email template
> > 
> > exception\_mail\_template = "/opt/chef/mail\_hanlder\_template.erb"
> > 
> > cookbook\_file "#{exception\_mail\_template}" do  
> > source "mail\_handler.erb"  
> > end
> > 
> > include\_recipe "chef-client::config"
> > 
> > Thanks
> 
> Attribute Precedence is probably what's screwing you up:  
> [About Attributes](http://docs.getchef.com/attributes.html#attribute-precedence)
> 
> Your default attribute in your attributes file would have the same  
> precedence as the cookbook. So order wins. A role will win though.

---

<div class="post-metadata">

**Author:** ![Peter\_Burkholder1](https://avatars.discourse-cdn.com/v4/letter/p/7ea924/32.png) [@Peter\_Burkholder1](https://discourse.chef.io/u/Peter_Burkholder1)\
**Post date:** [December 4, 2014, 8:25pm UTC](https://discourse.chef.io/t/defining-attributes-for-cookbook-in-recipe-has-different-behavior-than-attributes-from-roles/6144/4 "2014-12-04T20:25:31Z")

</div>

William,

You'll need to provide more detail about what's in your role, what you  
expect to see happen, what actually happens. Debug output would be good too  
(a Gist would be fine). Use markup in the Gist so we can see what's what  
more easily in the role, etc.

Note, you misspell mail\_handler as 'mail\_hanlder', which may be a  
confounding factor.

Avoid using attribute arrays: [Arrays and Chef Attributes – Noah Kantrowitz](https://coderanger.net/arrays-and-chef/)

The force\_override shouldn't be needed since the wrapper cookbook  
attributes/default.rb will load after the chef-client/attributes/default.rb  
file

Cheers,

Peter

> On Wed, Dec 3, 2014 at 11:37 PM, William Jimenez \<  
> [william.jimenez@itsoninc.com](mailto:william.jimenez@itsoninc.com)\> wrote:
> 
> > Hello
> > 
> > 1. I'm using the opscode chef-client cookbook, and I am trying to  
> > define attributes for the cookbook from a wrapper recipe, however they  
> > don't seem to be honored. When I define them in a role the node is in, it  
> > works however (working meaning chef\_client operates on the attributes).  
> > What is different about the two?
> > 
> > Here is my attributes file:
> > 
> > exception\_mail\_template = "/opt/chef/mail\_hanlder\_template.erb"
> > 
> > default['chef-client']['config']['exception\_handlers'] = [  
> > {"class" =\> "MailHandler", "arguments" =\> [{:template\_path =\>  
> > "#{exception\_mail\_template}"}]  
> > }]  
> > default['chef-client']['load\_gems'] = [  
> > {"chef-handler-mail" =\> {"require\_name" =\> "chef/handler/mail"}  
> > }]
> > 
> > and portion of my recipe where I call chef\_client
> > 
> > # Exception handling for chef client runs
> > 
> > chef\_gem "chef-handler-mail" do  
> > action :install  
> > end
> > 
> > # Install exception handler email template
> > 
> > exception\_mail\_template = "/opt/chef/mail\_hanlder\_template.erb"
> > 
> > cookbook\_file "#{exception\_mail\_template}" do  
> > source "mail\_handler.erb"  
> > end
> > 
> > include\_recipe "chef-client::config"
> > 
> > Thanks
> 
> Attribute Precedence is probably what's screwing you up:  
> [About Attributes](http://docs.getchef.com/attributes.html#attribute-precedence)
> 
> Your default attribute in your attributes file would have the same  
> precedence as the cookbook. So order wins. A role will win though.

---

<div class="post-metadata">

**Author:** ![William\_Jimenez](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/william_jimenez/32/181_2.png) [@William\_Jimenez](https://discourse.chef.io/u/William_Jimenez)\
**Post date:** [December 5, 2014, 3:14am UTC](https://discourse.chef.io/t/defining-attributes-for-cookbook-in-recipe-has-different-behavior-than-attributes-from-roles/6144/5 "2014-12-05T03:14:17Z")

</div>

I'm trying to configure an exception handler to receive notification when a  
chef run fails. I'm using the opscode cookbook chef-client to define this  
exception handler in the client.rb file of each chef node.

I expect when I call chef-client::config it to modify the client.rb to  
contain the following:

["chef/handler/mail"].each do |lib|

> begin  
> require lib  
> rescue LoadError  
> Chef::Log.warn "Failed to load #{lib}. This should be resolved after a  
> chef run."  
> end  
> end

exception\_handlers \<\< MailHandler.new()

and it does this just fine when I define the attributes in the role of a  
given node. But when I try to define the attributes in the recipe instead,  
it doesn't add the above changes to client.rb, only builds the file with  
the default settings.

The role has this defined in it (under the default attributes):

```
"chef_client": {

```

> ```
> "config": {
> "exception_handlers": [
> {
> "class": "MailHandler",
> "arguments": [
> ]
> }
> ]
> },
> "load_gems": {
> "chef-handler-mail": {
> "require_name": "chef/handler/mail"
> }
> }
> }
> 
> ```

Thanks for your help with this,

William

On Thu, Dec 4, 2014 at 12:25 PM, Peter Burkholder [pburkholder@getchef.com](mailto:pburkholder@getchef.com)  
wrote:

> William,
> 
> You'll need to provide more detail about what's in your role, what you  
> expect to see happen, what actually happens. Debug output would be good too  
> (a Gist would be fine). Use markup in the Gist so we can see what's what  
> more easily in the role, etc.
> 
> Note, you misspell mail\_handler as 'mail\_hanlder', which may be a  
> confounding factor.
> 
> Avoid using attribute arrays: [Arrays and Chef Attributes – Noah Kantrowitz](https://coderanger.net/arrays-and-chef/)
> 
> The force\_override shouldn't be needed since the wrapper cookbook  
> attributes/default.rb will load after the chef-client/attributes/default.rb  
> file
> 
> Cheers,
> 
> Peter
> 
> > On Wed, Dec 3, 2014 at 11:37 PM, William Jimenez \<  
> > [william.jimenez@itsoninc.com](mailto:william.jimenez@itsoninc.com)\> wrote:
> > 
> > > Hello
> > > 
> > > 1. I'm using the opscode chef-client cookbook, and I am trying to  
> > > define attributes for the cookbook from a wrapper recipe, however they  
> > > don't seem to be honored. When I define them in a role the node is in, it  
> > > works however (working meaning chef\_client operates on the attributes).  
> > > What is different about the two?
> > > 
> > > Here is my attributes file:
> > > 
> > > exception\_mail\_template = "/opt/chef/mail\_hanlder\_template.erb"
> > > 
> > > default['chef-client']['config']['exception\_handlers'] = [  
> > > {"class" =\> "MailHandler", "arguments" =\> [{:template\_path =\>  
> > > "#{exception\_mail\_template}"}]  
> > > }]  
> > > default['chef-client']['load\_gems'] = [  
> > > {"chef-handler-mail" =\> {"require\_name" =\> "chef/handler/mail"}  
> > > }]
> > > 
> > > and portion of my recipe where I call chef\_client
> > > 
> > > # Exception handling for chef client runs
> > > 
> > > chef\_gem "chef-handler-mail" do  
> > > action :install  
> > > end
> > > 
> > > # Install exception handler email template
> > > 
> > > exception\_mail\_template = "/opt/chef/mail\_hanlder\_template.erb"
> > > 
> > > cookbook\_file "#{exception\_mail\_template}" do  
> > > source "mail\_handler.erb"  
> > > end
> > > 
> > > include\_recipe "chef-client::config"
> > > 
> > > Thanks
> > 
> > Attribute Precedence is probably what's screwing you up:  
> > [About Attributes](http://docs.getchef.com/attributes.html#attribute-precedence)
> > 
> > Your default attribute in your attributes file would have the same  
> > precedence as the cookbook. So order wins. A role will win though.

--  
William Jimenez  
Systems Engineer, Operations  
ItsOn, Inc.  
650-241-8470 {us/pacific}

---

<div class="post-metadata">

**Author:** ![William\_Jimenez](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/william_jimenez/32/181_2.png) [@William\_Jimenez](https://discourse.chef.io/u/William_Jimenez)\
**Post date:** [December 5, 2014, 6:49am UTC](https://discourse.chef.io/t/defining-attributes-for-cookbook-in-recipe-has-different-behavior-than-attributes-from-roles/6144/6 "2014-12-05T06:49:31Z")

</div>

Hello all,  
I may have discovered the culprit, I discovered there is something for  
chef-client in the override attribute section of our roles, but it wasn't  
for the chef-client recipe (which is why I didn't expect it), but for the  
bootstrapper. It is probably wiping out my attempt to set the attributes in  
the recipe.

Thanks,  
William

On Thu, Dec 4, 2014 at 7:14 PM, William Jimenez \<  
[william.jimenez@itsoninc.com](mailto:william.jimenez@itsoninc.com)\> wrote:

> I'm trying to configure an exception handler to receive notification when  
> a chef run fails. I'm using the opscode cookbook chef-client to define this  
> exception handler in the client.rb file of each chef node.
> 
> I expect when I call chef-client::config it to modify the client.rb to  
> contain the following:
> 
> ["chef/handler/mail"].each do |lib|
> 
> > begin  
> > require lib  
> > rescue LoadError  
> > Chef::Log.warn "Failed to load #{lib}. This should be resolved after  
> > a chef run."  
> > end  
> > end
> 
> exception\_handlers \<\< MailHandler.new()
> 
> and it does this just fine when I define the attributes in the role of a  
> given node. But when I try to define the attributes in the recipe instead,  
> it doesn't add the above changes to client.rb, only builds the file with  
> the default settings.
> 
> The role has this defined in it (under the default attributes):
> 
> ```
> "chef_client": {
> 
> ```
> 
> > ```
> > "config": {
> > "exception_handlers": [
> > {
> > "class": "MailHandler",
> > "arguments": [
> > ]
> > }
> > ]
> > },
> > "load_gems": {
> > "chef-handler-mail": {
> > "require_name": "chef/handler/mail"
> > }
> > }
> > }
> > 
> > ```
> 
> Thanks for your help with this,
> 
> William
> 
> On Thu, Dec 4, 2014 at 12:25 PM, Peter Burkholder \<[pburkholder@getchef.com](mailto:pburkholder@getchef.com)
> 
> > wrote:
> 
> > William,
> > 
> > You'll need to provide more detail about what's in your role, what you  
> > expect to see happen, what actually happens. Debug output would be good too  
> > (a Gist would be fine). Use markup in the Gist so we can see what's what  
> > more easily in the role, etc.
> > 
> > Note, you misspell mail\_handler as 'mail\_hanlder', which may be a  
> > confounding factor.
> > 
> > Avoid using attribute arrays: [Arrays and Chef Attributes – Noah Kantrowitz](https://coderanger.net/arrays-and-chef/)
> > 
> > The force\_override shouldn't be needed since the wrapper cookbook  
> > attributes/default.rb will load after the chef-client/attributes/default.rb  
> > file
> > 
> > Cheers,
> > 
> > Peter
> > 
> > > On Wed, Dec 3, 2014 at 11:37 PM, William Jimenez \<  
> > > [william.jimenez@itsoninc.com](mailto:william.jimenez@itsoninc.com)\> wrote:
> > > 
> > > > Hello
> > > > 
> > > > 1. I'm using the opscode chef-client cookbook, and I am trying to  
> > > > define attributes for the cookbook from a wrapper recipe, however they  
> > > > don't seem to be honored. When I define them in a role the node is in, it  
> > > > works however (working meaning chef\_client operates on the attributes).  
> > > > What is different about the two?
> > > > 
> > > > Here is my attributes file:
> > > > 
> > > > exception\_mail\_template = "/opt/chef/mail\_hanlder\_template.erb"
> > > > 
> > > > default['chef-client']['config']['exception\_handlers'] = [  
> > > > {"class" =\> "MailHandler", "arguments" =\> [{:template\_path =\>  
> > > > "#{exception\_mail\_template}"}]  
> > > > }]  
> > > > default['chef-client']['load\_gems'] = [  
> > > > {"chef-handler-mail" =\> {"require\_name" =\> "chef/handler/mail"}  
> > > > }]
> > > > 
> > > > and portion of my recipe where I call chef\_client
> > > > 
> > > > # Exception handling for chef client runs
> > > > 
> > > > chef\_gem "chef-handler-mail" do  
> > > > action :install  
> > > > end
> > > > 
> > > > # Install exception handler email template
> > > > 
> > > > exception\_mail\_template = "/opt/chef/mail\_hanlder\_template.erb"
> > > > 
> > > > cookbook\_file "#{exception\_mail\_template}" do  
> > > > source "mail\_handler.erb"  
> > > > end
> > > > 
> > > > include\_recipe "chef-client::config"
> > > > 
> > > > Thanks
> > > 
> > > Attribute Precedence is probably what's screwing you up:  
> > > [About Attributes](http://docs.getchef.com/attributes.html#attribute-precedence)
> > > 
> > > Your default attribute in your attributes file would have the same  
> > > precedence as the cookbook. So order wins. A role will win though.
> 
> --  
> William Jimenez  
> Systems Engineer, Operations  
> ItsOn, Inc.  
> 650-241-8470 {us/pacific}

--  
William Jimenez  
Systems Engineer, Operations  
ItsOn, Inc.  
650-241-8470 {us/pacific}
