# Using chef resource in library

**URL:** <https://discourse.chef.io/t/using-chef-resource-in-library/5059>\
**Category:** Chef Infra (archive)\
**Created:** [February 21, 2014, 1:17am UTC](https://discourse.chef.io/t/using-chef-resource-in-library/5059 "2014-02-21T01:17:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ameya\_Savale](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/ameya_savale/32/9_2.png) [@Ameya\_Savale](https://discourse.chef.io/u/Ameya_Savale)\
**Post date:** [February 21, 2014, 1:17am UTC](https://discourse.chef.io/t/using-chef-resource-in-library/5059/1 "2014-02-21T01:17:52Z")

</div>

Hi Guys,

I was wondering if it was possible to use a chef resource such as ‘file’ or  
’cookbook\_file’ in a custom library. I have tried the following and none of  
them have been successful:

class Chef  
class Recipe  
class myhelper

file “…” do

end

class Chef  
class Resource  
class myhelper

file “…” do

end

class Chef  
class Resource  
class myhelper \< ::Chef::Resource

file “…” do

end

-thanks

---

<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:** [February 21, 2014, 2:36am UTC](https://discourse.chef.io/t/using-chef-resource-in-library/5059/2 "2014-02-21T02:36:17Z")

</div>

Library code is just 100% plain ruby code, so you can't use the recipe dsl unless you do special stuff to set that up. If you are trying to write class based resources using the lwp dsl, check out Poise and some of the cookbooks using it.

--Noah

Ameya Savale [ameya@simplyhired.com](mailto:ameya@simplyhired.com) wrote:

> Hi Guys,
> 
> I was wondering if it was possible to use a chef resource such as  
> 'file' or  
> 'cookbook\_file' in a custom library. I have tried the following and  
> none of  
> them have been successful:
> 
> class Chef  
> class Recipe  
> class myhelper
> 
> file "..." do
> 
> end
> 
> class Chef  
> class Resource  
> class myhelper
> 
> file "..." do
> 
> end
> 
> class Chef  
> class Resource  
> class myhelper \< ::Chef::Resource
> 
> file "..." do
> 
> end
> 
> -thanks

---

<div class="post-metadata">

**Author:** ![Ameya\_Savale](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/ameya_savale/32/9_2.png) [@Ameya\_Savale](https://discourse.chef.io/u/Ameya_Savale)\
**Post date:** [February 21, 2014, 6:05pm UTC](https://discourse.chef.io/t/using-chef-resource-in-library/5059/3 "2014-02-21T18:05:51Z")

</div>

thanks Noah

On Thu, Feb 20, 2014 at 6:36 PM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net)wrote:

> Library code is just 100% plain ruby code, so you can't use the recipe dsl  
> unless you do special stuff to set that up. If you are trying to write  
> class based resources using the lwp dsl, check out Poise and some of the  
> cookbooks using it.
> 
> --Noah
> 
> Ameya Savale [ameya@simplyhired.com](mailto:ameya@simplyhired.com) wrote:
> 
> > Hi Guys,
> > 
> > I was wondering if it was possible to use a chef resource such as 'file'  
> > or 'cookbook\_file' in a custom library. I have tried the following and none  
> > of them have been successful:
> > 
> > class Chef  
> > class Recipe  
> > class myhelper
> > 
> > file "..." do
> > 
> > end
> > 
> > class Chef  
> > class Resource  
> > class myhelper
> > 
> > file "..." do
> > 
> > end
> > 
> > class Chef  
> > class Resource  
> > class myhelper \< ::Chef::Resource
> > 
> > file "..." do
> > 
> > end
> > 
> > -thanks

---

<div class="post-metadata">

**Author:** ![Ranjib](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/ranjib/32/1040_2.png) [@Ranjib](https://discourse.chef.io/u/Ranjib)\
**Post date:** [February 21, 2014, 9:01pm UTC](https://discourse.chef.io/t/using-chef-resource-in-library/5059/4 "2014-02-21T21:01:27Z")

</div>

take a look at the chef-apply code[1],  
all resources in chef requires a run context, you can learn how to build  
run context from that code, after that either you can take the recipe route  
(i.e. instance\_eval ruby code having file, cookbook\_file etc, using  
Chef::Recipe) or the explicit Chef::File.new('/path',  
rc).run\_action(:create) route

cheers  
ranjib  
[1][https://github.com/opscode/chef/blob/master/lib/chef/application/apply.rb](https://github.com/opscode/chef/blob/master/lib/chef/application/apply.rb)

On Thu, Feb 20, 2014 at 5:17 PM, Ameya Savale [ameya@simplyhired.com](mailto:ameya@simplyhired.com) wrote:

> Hi Guys,
> 
> I was wondering if it was possible to use a chef resource such as 'file'  
> or 'cookbook\_file' in a custom library. I have tried the following and none  
> of them have been successful:
> 
> class Chef  
> class Recipe  
> class myhelper
> 
> file "..." do
> 
> end
> 
> class Chef  
> class Resource  
> class myhelper
> 
> file "..." do
> 
> end
> 
> class Chef  
> class Resource  
> class myhelper \< ::Chef::Resource
> 
> file "..." do
> 
> end
> 
> -thanks
