# Execute a single Chef recipe with chef-recipe command

**URL:** <https://discourse.chef.io/t/execute-a-single-chef-recipe-with-chef-recipe-command/3312>\
**Category:** Chef Infra (archive)\
**Created:** [November 18, 2012, 9:58am UTC](https://discourse.chef.io/t/execute-a-single-chef-recipe-with-chef-recipe-command/3312 "2012-11-18T09:58:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bryan\_Berry](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/bryan_berry/32/213_2.png) [@Bryan\_Berry](https://discourse.chef.io/u/Bryan_Berry)\
**Post date:** [November 18, 2012, 9:58am UTC](https://discourse.chef.io/t/execute-a-single-chef-recipe-with-chef-recipe-command/3312/1 "2012-11-18T09:58:53Z")

</div>

Hey Chefs,

I have started training more ppl at $DAYJOB and found that I need a quicker  
way get new users started with Chef. I usually only have 20-30 minutes  
maximum to get a user started with Chef. Explaining resources, recipes,  
cookbooks, roles, environments, etc. easily takes up 1 hour. I needed a way  
to give my users comfortable w/ the ideas of resources and the concept of a  
recipe w/ in w/in 20 minutes. The user also needs to do something hands-on  
w/in that 20 minutes. In the 2nd training session i would introduce  
chef-solo and cookbooks.

To this end I have created chef-recipe  
[https://github.com/bryanwb/chef-recipe](https://github.com/bryanwb/chef-recipe), which evaluates a single recipe  
`chef-recipe RECIPE_FILE` . It is largely based on a gist from Daniel  
DeLeo (thanks Dan!) It works pretty much the same as puppet-apply.

You can install chef-recipe as a gem `gem install chef-recipe`

chef-recipe is meant only as a training tool and not to be used for any  
real work. It is just a stepping stone to learning chef-solo and chef-client

This is totally alpha and I would love some feedback!

---

<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:** [November 18, 2012, 7:14pm UTC](https://discourse.chef.io/t/execute-a-single-chef-recipe-with-chef-recipe-command/3312/2 "2012-11-18T19:14:30Z")

</div>

On Sunday, November 18, 2012 at 1:58 AM, Bryan Berry wrote:

> Hey Chefs,
> 
> I have started training more ppl at $DAYJOB and found that I need a quicker way get new users started with Chef. I usually only have 20-30 minutes maximum to get a user started with Chef. Explaining resources, recipes, cookbooks, roles, environments, etc. easily takes up 1 hour. I needed a way to give my users comfortable w/ the ideas of resources and the concept of a recipe w/ in w/in 20 minutes. The user also needs to do something hands-on w/in that 20 minutes. In the 2nd training session i would introduce chef-solo and cookbooks.
> 
> To this end I have created chef-recipe [GitHub - bryanwb/chef-recipe: a command-line tool to execute a single chef recipe](https://github.com/bryanwb/chef-recipe), which evaluates a single recipe `chef-recipe RECIPE_FILE` . It is largely based on a gist from Daniel DeLeo (thanks Dan!) It works pretty much the same as puppet-apply.
> 
> You can install chef-recipe as a gem `gem install chef-recipe`
> 
> chef-recipe is meant only as a training tool and not to be used for any real work. It is just a stepping stone to learning chef-solo and chef-client
> 
> This is totally alpha and I would love some feedback!  
> This is most of the way towards what we'd need to merge it into chef proper. Just needs some tests and to take the monkey patching stuff out. Interested in contributing it?

--  
Daniel DeLeo

---

<div class="post-metadata">

**Author:** ![Bryan\_Berry](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/bryan_berry/32/213_2.png) [@Bryan\_Berry](https://discourse.chef.io/u/Bryan_Berry)\
**Post date:** [November 18, 2012, 8:04pm UTC](https://discourse.chef.io/t/execute-a-single-chef-recipe-with-chef-recipe-command/3312/3 "2012-11-18T20:04:49Z")

</div>

I would be happy to try. Forgive me if I ask dumb questions, working w/  
real Ruby libraries is still a new experience for me.

To be honest, the part of this code that I understand the least is the  
monkey-patch from your orginal gist

class Chef::Client  
attr\_reader :events  
end

I assume that to get rid of the monkey patch, I would just add `attr_reader :events` directly to lib/chef/client.rb, correct?

by tests, are you looking for something like this?

> <https://github.com/chef/chef/blob/main/spec/unit/application/solo_spec.rb>

tks again for the original gist.

I would also like to merge in my changes from chef-rewind. Keep meaning to  
do that

On Sun, Nov 18, 2012 at 8:14 PM, Daniel DeLeo [dan@kallistec.com](mailto:dan@kallistec.com) wrote:

> On Sunday, November 18, 2012 at 1:58 AM, Bryan Berry wrote:
> 
> Hey Chefs,
> 
> I have started training more ppl at $DAYJOB and found that I need a  
> quicker way get new users started with Chef. I usually only have 20-30  
> minutes maximum to get a user started with Chef. Explaining resources,  
> recipes, cookbooks, roles, environments, etc. easily takes up 1 hour. I  
> needed a way to give my users comfortable w/ the ideas of resources and the  
> concept of a recipe w/ in w/in 20 minutes. The user also needs to do  
> something hands-on w/in that 20 minutes. In the 2nd training session i  
> would introduce chef-solo and cookbooks.
> 
> To this end I have created chef-recipe  
> [GitHub - bryanwb/chef-recipe: a command-line tool to execute a single chef recipe](https://github.com/bryanwb/chef-recipe), which evaluates a single recipe  
> `chef-recipe RECIPE_FILE` . It is largely based on a gist from Daniel  
> DeLeo (thanks Dan!) It works pretty much the same as puppet-apply.
> 
> You can install chef-recipe as a gem `gem install chef-recipe`
> 
> chef-recipe is meant only as a training tool and not to be used for any  
> real work. It is just a stepping stone to learning chef-solo and chef-client
> 
> This is totally alpha and I would love some feedback!
> 
> This is most of the way towards what we'd need to merge it into chef  
> proper. Just needs some tests and to take the monkey patching stuff out.  
> Interested in contributing it?
> 
> --  
> Daniel DeLeo

---

<div class="post-metadata">

**Author:** ![Joshua\_Timberman](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/joshua_timberman/32/424_2.png) [@Joshua\_Timberman](https://discourse.chef.io/u/Joshua_Timberman)\
**Post date:** [November 18, 2012, 8:06pm UTC](https://discourse.chef.io/t/execute-a-single-chef-recipe-with-chef-recipe-command/3312/4 "2012-11-18T20:06:18Z")

</div>

Ohai,

On 11/18/12 2:58 AM, "Bryan Berry" [bryan.berry@gmail.com](mailto:bryan.berry@gmail.com) wrote:

> To this end I have created chef-recipe  
> [GitHub - bryanwb/chef-recipe: a command-line tool to execute a single chef recipe](https://github.com/bryanwb/chef-recipe), which evaluates a single recipe  
> `chef-recipe RECIPE_FILE` . It is largely based on a gist from Daniel  
> DeLeo (thanks Dan!)  
> It works pretty much the same as puppet-apply.

As Dan said, this is something we'd like to have in Chef proper. There's a  
ticket for adding such a feature here:

[http://tickets.opscode.com/browse/CHEF-3571](http://tickets.opscode.com/browse/CHEF-3571)

If you'd like to contribute, please use that ticket. Thanks!

---

<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:** [November 18, 2012, 8:20pm UTC](https://discourse.chef.io/t/execute-a-single-chef-recipe-with-chef-recipe-command/3312/5 "2012-11-18T20:20:57Z")

</div>

On Sunday, November 18, 2012 at 12:04 PM, Bryan Berry wrote:

> I would be happy to try. Forgive me if I ask dumb questions, working w/ real Ruby libraries is still a new experience for me.
> 
> To be honest, the part of this code that I understand the least is the monkey-patch from your orginal gist
> 
> class Chef::Client  
> attr\_reader :events  
> end
> 
> I assume that to get rid of the monkey patch, I would just add `attr_reader :events` directly to lib/chef/client.rb, correct?  
> Short answer: yes. Longer answer:

Recall that in ruby, classes are "open" so you can add or overwrite methods/constants at any time. Here you're just adding Client#events (which returns @events) to the class.

Chef::Client#initialize runs `EventDispatch::Dispatcher.new(formatter)` to create the event dispatcher, and later creates the RunContext with the event dispatcher, but since we need to customize RunContext compared to what Chef::Client normally does, we need to get the event dispatcher out of the client so it can be passed in to RunContext#initialize. This could have been done with `instance_variable_get` in the original, but I like that even less than monkey patching.

> by tests, are you looking for something like this? [chef/spec/unit/application/solo\_spec.rb at main · chef/chef · GitHub](https://github.com/opscode/chef/blob/master/spec/unit/application/solo_spec.rb)

Ideally we'd have a few levels of tests. Making sure that the command line options effect the desired config changes via unit tests is good. I'd also like to see a functional (in spec/functional) test that runs a simple recipe (single file resource is fine) to ensure that everything is correctly wired up all the way through.

> tks again for the original gist.
> 
> I would also like to merge in my changes from chef-rewind. Keep meaning to do that

--  
Daniel DeLeo

---

<div class="post-metadata">

**Author:** ![Jon\_Stemmle](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@Jon\_Stemmle](https://discourse.chef.io/u/Jon_Stemmle)\
**Post date:** [November 18, 2012, 8:51pm UTC](https://discourse.chef.io/t/execute-a-single-chef-recipe-with-chef-recipe-command/3312/6 "2012-11-18T20:51:04Z")

</div>

Just out of if this is not meant to be used in prime-time for "any real  
work", what is the "chef" way of handling the cases where you want to run  
some scripts you have just one time manually on a set of servers?

Thanks!  
Jon  
@stemmle

On Sun, Nov 18, 2012 at 1:58 AM, Bryan Berry [bryan.berry@gmail.com](mailto:bryan.berry@gmail.com) wrote:

> Hey Chefs,
> 
> I have started training more ppl at $DAYJOB and found that I need a  
> quicker way get new users started with Chef. I usually only have 20-30  
> minutes maximum to get a user started with Chef. Explaining resources,  
> recipes, cookbooks, roles, environments, etc. easily takes up 1 hour. I  
> needed a way to give my users comfortable w/ the ideas of resources and the  
> concept of a recipe w/ in w/in 20 minutes. The user also needs to do  
> something hands-on w/in that 20 minutes. In the 2nd training session i  
> would introduce chef-solo and cookbooks.
> 
> To this end I have created chef-recipe  
> [GitHub - bryanwb/chef-recipe: a command-line tool to execute a single chef recipe](https://github.com/bryanwb/chef-recipe), which evaluates a single recipe  
> `chef-recipe RECIPE_FILE` . It is largely based on a gist from Daniel  
> DeLeo (thanks Dan!) It works pretty much the same as puppet-apply.
> 
> You can install chef-recipe as a gem `gem install chef-recipe`
> 
> chef-recipe is meant only as a training tool and not to be used for any  
> real work. It is just a stepping stone to learning chef-solo and chef-client
> 
> This is totally alpha and I would love some feedback!

---

<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:** [November 18, 2012, 8:54pm UTC](https://discourse.chef.io/t/execute-a-single-chef-recipe-with-chef-recipe-command/3312/7 "2012-11-18T20:54:30Z")

</div>

On Nov 18, 2012, at 12:51 PM, Jon Stemmle wrote:

> Just out of if this is not meant to be used in prime-time for "any real work", what is the "chef" way of handling the cases where you want to run some scripts you have just one time manually on a set of servers?

knife ssh would be the built-in way. Personally I use fabric for that though.

--Noah

---

<div class="post-metadata">

**Author:** ![Bryan\_Berry](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/bryan_berry/32/213_2.png) [@Bryan\_Berry](https://discourse.chef.io/u/Bryan_Berry)\
**Post date:** [November 18, 2012, 8:55pm UTC](https://discourse.chef.io/t/execute-a-single-chef-recipe-with-chef-recipe-command/3312/8 "2012-11-18T20:55:03Z")

</div>

I have a utility cookbook where I place just such scripts in an individual  
recipe

for example, the /usr/localt/tomcat/webapps folder must have permissions  
set so that Jenkins can automatically deploy to it.

Some of my applications are 100% managed by Chef but some are not. For  
those that aren't i created a recipe

utility::jenkins\_pems.rb

If u do write such a script, do your best to make sure it is idempotent.  
80% of the time I think a script is a one-off, it ends up not being so

On Sun, Nov 18, 2012 at 9:51 PM, Jon Stemmle [jstemmle@topspinmedia.com](mailto:jstemmle@topspinmedia.com)wrote:

> Just out of if this is not meant to be used in prime-time for "any real  
> work", what is the "chef" way of handling the cases where you want to run  
> some scripts you have just one time manually on a set of servers?
> 
> Thanks!  
> Jon  
> @stemmle
> 
> On Sun, Nov 18, 2012 at 1:58 AM, Bryan Berry [bryan.berry@gmail.com](mailto:bryan.berry@gmail.com)wrote:
> 
> > Hey Chefs,
> > 
> > I have started training more ppl at $DAYJOB and found that I need a  
> > quicker way get new users started with Chef. I usually only have 20-30  
> > minutes maximum to get a user started with Chef. Explaining resources,  
> > recipes, cookbooks, roles, environments, etc. easily takes up 1 hour. I  
> > needed a way to give my users comfortable w/ the ideas of resources and the  
> > concept of a recipe w/ in w/in 20 minutes. The user also needs to do  
> > something hands-on w/in that 20 minutes. In the 2nd training session i  
> > would introduce chef-solo and cookbooks.
> > 
> > To this end I have created chef-recipe  
> > [GitHub - bryanwb/chef-recipe: a command-line tool to execute a single chef recipe](https://github.com/bryanwb/chef-recipe), which evaluates a single recipe  
> > `chef-recipe RECIPE_FILE` . It is largely based on a gist from Daniel  
> > DeLeo (thanks Dan!) It works pretty much the same as puppet-apply.
> > 
> > You can install chef-recipe as a gem `gem install chef-recipe`
> > 
> > chef-recipe is meant only as a training tool and not to be used for any  
> > real work. It is just a stepping stone to learning chef-solo and chef-client
> > 
> > This is totally alpha and I would love some feedback!
