# Running knife bootstrap programatically

**URL:** <https://discourse.chef.io/t/running-knife-bootstrap-programatically/1988>\
**Category:** Chef Infra (archive)\
**Created:** [August 3, 2011, 9:31am UTC](https://discourse.chef.io/t/running-knife-bootstrap-programatically/1988 "2011-08-03T09:31:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haim\_Ashkenazi](https://avatars.discourse-cdn.com/v4/letter/h/ed655f/32.png) [@Haim\_Ashkenazi](https://discourse.chef.io/u/Haim_Ashkenazi)\
**Post date:** [August 3, 2011, 9:31am UTC](https://discourse.chef.io/t/running-knife-bootstrap-programatically/1988/1 "2011-08-03T09:31:38Z")

</div>

Hi

I was wondering if there’s a way to run knife bootstrap not as a command,  
but as API call? (Sorry if it’s already been answered but it’s really hard  
to search the mailing list).

I tried using something simple like (ignore the redefinition of ARGV :):

require 'chef/knife/bootstrap’  
ARGV = [“some.host”, “-x”, “ubuntu”, “-r”, “role[test]”, “–sudo”]  
Chef:🔪:Bootstrap.new.run

Running this I got an error:  
/Users/haim/.rvm/gems/ruby-1.8.7-p352@chef\_010/gems/chef-0.10.2/lib/chef/knife/core/ui.rb:96:  
warning: toplevel constant Config referenced by Chef::Config  
/Users/haim/.rvm/gems/ruby-1.8.7-p352@chef\_010/gems/chef-0.10.2/lib/chef/knife/core/ui.rb:96:in  
`color?': undefined method`[]’ for Config:Module (NoMethodError)  
from /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef\_010/gems/chef-0.10.2/lib/chef/knife/core/ui.rb:85:in  
`color' from /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef_010/gems/chef-0.10.2/lib/chef/knife/core/ui.rb:76:in`error’  
from /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef\_010/gems/chef-0.10.2/lib/chef/knife/bootstrap.rb:158:in  
`validate_name_args!' from /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef_010/gems/chef-0.10.2/lib/chef/knife/bootstrap.rb:137:in`run’  
from test.rb:4

Is there any easy way to run it?

## Thanks in advance

Haim

---

<div class="post-metadata">

**Author:** ![Haim\_Ashkenazi](https://avatars.discourse-cdn.com/v4/letter/h/ed655f/32.png) [@Haim\_Ashkenazi](https://discourse.chef.io/u/Haim_Ashkenazi)\
**Post date:** [August 3, 2011, 10:05am UTC](https://discourse.chef.io/t/running-knife-bootstrap-programatically/1988/2 "2011-08-03T10:05:11Z")

</div>

Hi

The following code also raised the same error as in the original message:

require 'rubygems'  
require 'chef/knife/bootstrap'

kb = Chef:🔪:Bootstrap.new  
kb.name\_args = "[ec2-107-20-48-203.compute-1.amazonaws.com](http://ec2-107-20-48-203.compute-1.amazonaws.com)"  
kb.config[:ssh\_user] = "ubuntu"  
kb.config[:run\_list] = "role[test]"  
kb.config[:use\_sudo] = true  
kb.run

☹

Bye

On Wed, Aug 3, 2011 at 12:31 PM, Haim Ashkenazi [haim.ashkenazi@gmail.com](mailto:haim.ashkenazi@gmail.com)wrote:

> Hi
> 
> I was wondering if there's a way to run knife bootstrap not as a command,  
> but as API call? (Sorry if it's already been answered but it's really hard  
> to search the mailing list).
> 
> I tried using something simple like (ignore the redefinition of ARGV :):
> 
> require 'chef/knife/bootstrap'  
> ARGV = ["some.host", "-x", "ubuntu", "-r", "role[test]", "--sudo"]  
> Chef:🔪:Bootstrap.new.run
> 
> Running this I got an error:  
> /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef\_010/gems/chef-0.10.2/lib/chef/knife/core/ui.rb:96:  
> warning: toplevel constant Config referenced by Chef::Config  
> /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef\_010/gems/chef-0.10.2/lib/chef/knife/core/ui.rb:96:in  
> `color?': undefined method `' for Config:Module (NoMethodError)  
> from /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef\_010/gems/chef-0.10.2/lib/chef/knife/core/ui.rb:85:in  
> `color' from /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef_010/gems/chef-0.10.2/lib/chef/knife/core/ui.rb:76:in `error'  
> from /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef\_010/gems/chef-0.10.2/lib/chef/knife/bootstrap.rb:158:in  
> `validate_name_args!' from /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef_010/gems/chef-0.10.2/lib/chef/knife/bootstrap.rb:137:in `run'  
> from test.rb:4
> 
> Is there any easy way to run it?
> 
> ## Thanks in advance
> 
> Haim

--  
Haim

---

<div class="post-metadata">

**Author:** ![Haim\_Ashkenazi](https://avatars.discourse-cdn.com/v4/letter/h/ed655f/32.png) [@Haim\_Ashkenazi](https://discourse.chef.io/u/Haim_Ashkenazi)\
**Post date:** [August 3, 2011, 11:08am UTC](https://discourse.chef.io/t/running-knife-bootstrap-programatically/1988/3 "2011-08-03T11:08:47Z")

</div>

Ok, solved! thanks to the great guys in the IRC channel. Here's a way to run  
it (if someone is interested):

require 'rubygems'  
require "chef"  
require "chef/knife/core/bootstrap\_context"  
require 'chef/knife'  
require 'chef/knife/ssh'  
require 'net/ssh'  
require 'net/ssh/multi'  
require 'chef/knife/bootstrap'

Chef::Config.from\_file(File.expand\_path('~/.chef/knife.rb'))  
kb = Chef:🔪:Bootstrap.new  
kb.name\_args = "some.host"  
kb.config[:ssh\_user] = "ubuntu"  
kb.config[:run\_list] = "role[test]"  
kb.config[:use\_sudo] = true  
kb.run

Bye

On Wed, Aug 3, 2011 at 1:05 PM, Haim Ashkenazi [haim.ashkenazi@gmail.com](mailto:haim.ashkenazi@gmail.com)wrote:

> Hi
> 
> The following code also raised the same error as in the original message:
> 
> require 'rubygems'  
> require 'chef/knife/bootstrap'
> 
> kb = Chef:🔪:Bootstrap.new  
> kb.name\_args = "[ec2-107-20-48-203.compute-1.amazonaws.com](http://ec2-107-20-48-203.compute-1.amazonaws.com)"  
> kb.config[:ssh\_user] = "ubuntu"  
> kb.config[:run\_list] = "role[test]"  
> kb.config[:use\_sudo] = true  
> kb.run
> 
> ☹
> 
> Bye
> 
> On Wed, Aug 3, 2011 at 12:31 PM, Haim Ashkenazi [haim.ashkenazi@gmail.com](mailto:haim.ashkenazi@gmail.com)wrote:
> 
> > Hi
> > 
> > I was wondering if there's a way to run knife bootstrap not as a command,  
> > but as API call? (Sorry if it's already been answered but it's really hard  
> > to search the mailing list).
> > 
> > I tried using something simple like (ignore the redefinition of ARGV :):
> > 
> > require 'chef/knife/bootstrap'  
> > ARGV = ["some.host", "-x", "ubuntu", "-r", "role[test]", "--sudo"]  
> > Chef:🔪:Bootstrap.new.run
> > 
> > Running this I got an error:  
> > /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef\_010/gems/chef-0.10.2/lib/chef/knife/core/ui.rb:96:  
> > warning: toplevel constant Config referenced by Chef::Config  
> > /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef\_010/gems/chef-0.10.2/lib/chef/knife/core/ui.rb:96:in  
> > `color?': undefined method `' for Config:Module (NoMethodError)  
> > from /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef\_010/gems/chef-0.10.2/lib/chef/knife/core/ui.rb:85:in  
> > `color' from /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef_010/gems/chef-0.10.2/lib/chef/knife/core/ui.rb:76:in `error'  
> > from /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef\_010/gems/chef-0.10.2/lib/chef/knife/bootstrap.rb:158:in  
> > `validate_name_args!' from /Users/haim/.rvm/gems/ruby-1.8.7-p352@chef_010/gems/chef-0.10.2/lib/chef/knife/bootstrap.rb:137:in `run'  
> > from test.rb:4
> > 
> > Is there any easy way to run it?
> > 
> > ## Thanks in advance
> > 
> > Haim
> 
> --  
> Haim

--  
Haim
