Recent releases of chef from opscode provide a new resource: ruby_block

This is really great news as this allows for interesting problem solving.

Consider you want to do a very special HTTP request. E.g. a GET to localhost but with custom “Host” header information.

Install a Ruby Http Client

First we have to install a Ruby http client library. I’ll use curb in this example. It builds on top of libcurl so lets install that first.

curl_devel = package "curl-devel" do
  action :install


With this code we tell chef to immediately execute this code. Otherwise we would have to face the problem that we just fullfill the chefrun post-condition that we have curl-devel installed. Next we have to install the curb gem.

curb_gem = gem_package "curb" do
  action :install


as you see this is done the same way. Next we want to use curb in THIS chef-run. So we have tell chef the update of Gem, followed by requiring curb


require 'curb'

Ruby Code in Action

Now we are ready to take action.

ruby_block "do-http-request-with-cutom-header" do
  block do
    timeout = 600
    host = "localhost:8080"
    real_host = "" "call get on #{host}, maximal request time: #{timeout} seconds"
    c ="http://#{host}/maintenance/do_something") do |curl|
      curl.headers['Host'] = real_host
      curl.verbose = true
      curl.timeout = timeout
    if c.response_code == 200 "GET success! response was:#{c.body_str}"
      Chef::Log.error "GET FAILED. request response was HTTP #{c.response_code}, body: #{c.body_str}"
  action :create

Great. Isn’t it? :-)

Interested in Cloud or Chef Trainings? Have a look at our Commandemy Trainings page. Need help migrating to the cloud? Check out Infralovers.

comments powered by Disqus
Blog Tags