# Cookbook name as an attribute

**URL:** <https://discourse.chef.io/t/cookbook-name-as-an-attribute/4845>\
**Category:** Chef Infra (archive)\
**Created:** [December 2, 2013, 5:11pm UTC](https://discourse.chef.io/t/cookbook-name-as-an-attribute/4845 "2013-12-02T17:11:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Cook](https://avatars.discourse-cdn.com/v4/letter/t/c37758/32.png) [@Tim\_Cook](https://discourse.chef.io/u/Tim_Cook)\
**Post date:** [December 2, 2013, 5:11pm UTC](https://discourse.chef.io/t/cookbook-name-as-an-attribute/4845/1 "2013-12-02T17:11:51Z")

</div>

I am trying to pass the cookbook name to the logger via a library and have not been able to figure out how to pull that attribute, does anyone know how a cookbook calls its self in the form of an attribute ?

---

<div class="post-metadata">

**Author:** ![coderanger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/coderanger/32/5_2.png) [@coderanger](https://discourse.chef.io/u/coderanger)\
**Post date:** [December 2, 2013, 7:01pm UTC](https://discourse.chef.io/t/cookbook-name-as-an-attribute/4845/2 "2013-12-02T19:01:38Z")

</div>

On Dec 2, 2013, at 9:11 AM, Tim Cook [tim.cook@hightail.com](mailto:tim.cook@hightail.com) wrote:

> I am trying to pass the cookbook name to the logger via a library and have not been able to figure out how to pull that attribute, does anyone know how a cookbook calls its self in the form of an attribute ?

Within the context of a recipe or provider you can use #cookbook\_name (be warned it is sometimes a Symbol instead of a String). Within a library method you can't get to it though, so you would need to pass it in as a parameter.

--Noah

---

<div class="post-metadata">

**Author:** ![Adam\_Mielke](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/adam_mielke/32/434_2.png) [@Adam\_Mielke](https://discourse.chef.io/u/Adam_Mielke)\
**Post date:** [December 2, 2013, 5:28pm UTC](https://discourse.chef.io/t/cookbook-name-as-an-attribute/4845/3 "2013-12-02T17:28:44Z")

</div>

Within a cookbook the variable "cookbook\_name" should contain its own name.

Adam Mielke  
System Engineer  
Research Computing and Engineering / College of Liberal Arts  
University of Minnesota

On Mon, Dec 2, 2013 at 11:11 AM, Tim Cook [tim.cook@hightail.com](mailto:tim.cook@hightail.com) wrote:

> I am trying to pass the cookbook name to the logger via a library and  
> have not been able to figure out how to pull that attribute, does anyone  
> know how a cookbook calls its self in the form of an attribute ?

---

<div class="post-metadata">

**Author:** ![kallistec](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallistec/32/23_2.png) [@kallistec](https://discourse.chef.io/u/kallistec)\
**Post date:** [December 2, 2013, 5:30pm UTC](https://discourse.chef.io/t/cookbook-name-as-an-attribute/4845/4 "2013-12-02T17:30:37Z")

</div>

On Monday, December 2, 2013 at 9:28 AM, Adam Mielke wrote:

> Within a cookbook the variable "cookbook\_name" should contain its own name.

This won’t be true in library files, they are evaluated with Ruby’s `load` function so they don’t have the same contextual stuff that recipes do.

The best you can do here is look at ` __FILE__ ` and infer the cookbook name from the file path.

> Adam Mielke  
> System Engineer  
> Research Computing and Engineering / College of Liberal Arts  
> University of Minnesota
> 
> On Mon, Dec 2, 2013 at 11:11 AM, Tim Cook \<[tim.cook@hightail.com](mailto:tim.cook@hightail.com) ([mailto:tim.cook@hightail.com](mailto:tim.cook@hightail.com))\> wrote:
> 
> > I am trying to pass the cookbook name to the logger via a library and have not been able to figure out how to pull that attribute, does anyone know how a cookbook calls its self in the form of an attribute ?

--  
Daniel DeLeo

---

<div class="post-metadata">

**Author:** ![Tim\_Cook](https://avatars.discourse-cdn.com/v4/letter/t/c37758/32.png) [@Tim\_Cook](https://discourse.chef.io/u/Tim_Cook)\
**Post date:** [December 2, 2013, 5:34pm UTC](https://discourse.chef.io/t/cookbook-name-as-an-attribute/4845/5 "2013-12-02T17:34:23Z")

</div>

Cool thanks, worked perfect . I was calling it wrong .

From: Adam Mielke \<[adam@umn.edu](mailto:adam@umn.edu)[mailto:adam@umn.edu](mailto:adam@umn.edu)\>  
Reply-To: "chef@lists.opscode.com[mailto:chef@lists.opscode.com](mailto:chef@lists.opscode.com)" \<[chef@lists.opscode.com](mailto:chef@lists.opscode.com)[mailto:chef@lists.opscode.com](mailto:chef@lists.opscode.com)\>  
Date: Monday, December 2, 2013 9:28 AM  
To: "chef@lists.opscode.com[mailto:chef@lists.opscode.com](mailto:chef@lists.opscode.com)" \<[chef@lists.opscode.com](mailto:chef@lists.opscode.com)[mailto:chef@lists.opscode.com](mailto:chef@lists.opscode.com)\>  
Subject: [chef] Re: cookbook name as an attribute

Within a cookbook the variable “cookbook\_name” should contain its own name.

Adam Mielke  
System Engineer  
Research Computing and Engineering / College of Liberal Arts  
University of Minnesota

On Mon, Dec 2, 2013 at 11:11 AM, Tim Cook \<[tim.cook@hightail.com](mailto:tim.cook@hightail.com)[mailto:tim.cook@hightail.com](mailto:tim.cook@hightail.com)\> wrote:  
I am trying to pass the cookbook name to the logger via a library and have not been able to figure out how to pull that attribute, does anyone know how a cookbook calls its self in the form of an attribute ?

---

<div class="post-metadata">

**Author:** ![Tim\_Cook](https://avatars.discourse-cdn.com/v4/letter/t/c37758/32.png) [@Tim\_Cook](https://discourse.chef.io/u/Tim_Cook)\
**Post date:** [December 2, 2013, 5:44pm UTC](https://discourse.chef.io/t/cookbook-name-as-an-attribute/4845/6 "2013-12-02T17:44:47Z")

</div>

Why am I unable to pass the cookbook as an instance variable to a library class ?

From: Daniel DeLeo \<[dan@kallistec.com](mailto:dan@kallistec.com)[mailto:dan@kallistec.com](mailto:dan@kallistec.com)\>  
Reply-To: "chef@lists.opscode.com[mailto:chef@lists.opscode.com](mailto:chef@lists.opscode.com)" \<[chef@lists.opscode.com](mailto:chef@lists.opscode.com)[mailto:chef@lists.opscode.com](mailto:chef@lists.opscode.com)\>  
Date: Monday, December 2, 2013 9:30 AM  
To: "chef@lists.opscode.com[mailto:chef@lists.opscode.com](mailto:chef@lists.opscode.com)" \<[chef@lists.opscode.com](mailto:chef@lists.opscode.com)[mailto:chef@lists.opscode.com](mailto:chef@lists.opscode.com)\>  
Subject: [chef] Re: Re: cookbook name as an attribute

On Monday, December 2, 2013 at 9:28 AM, Adam Mielke wrote:  
Within a cookbook the variable “cookbook\_name” should contain its own name.

This won’t be true in library files, they are evaluated with Ruby’s `load` function so they don’t have the same contextual stuff that recipes do.

The best you can do here is look at ` __FILE__ ` and infer the cookbook name from the file path.

Adam Mielke  
System Engineer  
Research Computing and Engineering / College of Liberal Arts  
University of Minnesota

On Mon, Dec 2, 2013 at 11:11 AM, Tim Cook \<[tim.cook@hightail.com](mailto:tim.cook@hightail.com)[mailto:tim.cook@hightail.com](mailto:tim.cook@hightail.com)\> wrote:  
I am trying to pass the cookbook name to the logger via a library and have not been able to figure out how to pull that attribute, does anyone know how a cookbook calls its self in the form of an attribute ?

–  
Daniel DeLeo

---

<div class="post-metadata">

**Author:** ![kallistec](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallistec/32/23_2.png) [@kallistec](https://discourse.chef.io/u/kallistec)\
**Post date:** [December 2, 2013, 5:52pm UTC](https://discourse.chef.io/t/cookbook-name-as-an-attribute/4845/7 "2013-12-02T17:52:26Z")

</div>

On Monday, December 2, 2013 at 9:44 AM, Tim Cook wrote:

> Why am I unable to pass the cookbook as an instance variable to a library class ?

That you can do. In more exact terms:

recipes, attribute files, resource definitions, and LWRP files are all evaluated within a special context. For recipes, this is `instance_eval` of an already initialized `Chef::Recipe` object, which already has the node object, run\_context and other stuff populated as instance variables with accessor methods you can use.

library files, on the other hand, are evaluated just by calling ruby’s `Kernel.load` function so they don’t have any of this special context available, so if you need access to the node, run\_context or whatever, you have to pass it in from wherever the code gets called.

--  
Daniel DeLeo
