Counting bytes and measuring latencies

Consuming chef api from any script - part 1

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[: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