DevOps Zone is brought to you in partnership with:

Ranjib is a system administrator at Google. Prior to Google, Ranjib was a senior consultant with ThoughtWorks. He works on private cloud implementation strategies, cloud adoption, system automation etc. He has worked on both application development as well as system administration, for past 6 years. Prior to ThoughtWorks, Ranjib was working with Persistent Systems . Ranjib has done his gradation in lifescience and masters in Bioinformatics. Ranjib is a staunch FOSS supporter. Ranjib is a DZone MVB and is not an employee of DZone and has posted 13 posts at DZone. You can read more from them at their website. View Full User Profile

Consuming Chef API from Any Script - Part 1

03.31.2012
| 9141 views |
  • submit to reddit

Its been more than a year since we have adopted Chef for ThoughtWorks. We use Chef not only for configuration management but also for provisioning instances.  Since most of our infrastructure is automated many of the services needs to integrate with chef programmatically.

Knife, the command line interface of chef provides easy integration via knife plugin and Knife exec. But in this post I’ll showcase how Chef can be used from any script if you have an API client configured. At bare minimum we need to set three parameters to get our script talking to a Chef server, these are Chef server url, node_name and client key. Once configured we can use Chef API to interact with chef server. Lets start with enlisting the number of nodes

require 'rubygems'
require 'chef'  

Chef::Config[:node_name]='client_name'
Chef::Config[:client_key]='path to client cert.pem'
Chef::Config[:chef_server_url]="http://ur chef server:4000"     

Chef::Node.list.each do |node|  
    puts node.first      
end
Published at DZone with permission of Ranjib Dey, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)