Skip to main content

ruby_block Resource

This page is generated from the Chef Infra Client source code.
To suggest a change, edit the ruby_block.rb file and submit a pull request to the Chef Infra Client repository.

All Infra resources page

Use the ruby_block resource to execute Ruby code during a Chef Infra Client run. Ruby code in the ruby_block resource is evaluated with other resources during convergence, whereas Ruby code outside of a ruby_block resource is evaluated before other resources, as the recipe is compiled.


A ruby_block resource block executes a block of arbitrary Ruby code. For example, to reload the client.rb file during a Chef Infra Client run:

ruby_block 'reload_client_config' do
  block do
  action :run

The full syntax for all of the properties that are available to the ruby_block resource is:

ruby_block 'name' do
  block           Block
  block_name      String # default value: 'name' unless specified
  action          Symbol # defaults to :run if not specified


  • ruby_block is the resource.
  • name is the name given to the resource block.
  • block is the block of Ruby code to be executed.
  • action identifies which steps Chef Infra Client will take to bring the node into the desired state.
  • block and block_name are properties of this resource, with the Ruby type shown. See “Properties” section below for more information about all of the properties that may be used with this resource.


The ruby_block resource has the following actions:

The same as :run.
This resource block does not act unless notified by another resource to take action. Once notified, this resource block either runs immediately or is queued up to run at the end of a Chef Infra Client run.
Run a Ruby block. (default)


The ruby_block resource has the following properties:

Ruby Type: block

A block of Ruby code.

Ruby Type: String | Default Value: The resource block's name

The name of the Ruby block. Default value: the name of the resource block. See “Syntax” section above for more information.


The following examples demonstrate various approaches for using the ruby_block resource in recipes:

Reload Chef Infra Client configuration data

ruby_block 'reload_client_config' do
  block do
  action :run

Run a block on a particular platform

The following example shows how an if statement can be used with the windows? method in the Chef Infra Language to run code specific to Microsoft Windows. The code is defined using the ruby_block resource:

if windows?
  ruby_block 'copy libmysql.dll into ruby path' do
    block do
      require 'fileutils'
      FileUtils.cp "#{node['mysql']['client']['lib_dir']}\\libmysql.dll",
    not_if { ::File.exist?("#{node['mysql']['client']['ruby_dir']}\\libmysql.dll") }

Stash a file in a data bag

The following example shows how to use the ruby_block resource to stash a BitTorrent file in a data bag so that it can be distributed to nodes in the organization.

ruby_block 'share the torrent file' do
  block do
    f =['bittorrent']['torrent'],'rb')
    #read the .torrent file and base64 encode it
    enc = Base64.encode64(
    data = {
    item =
    item.raw_data = data
  action :nothing
  subscribes :create, "bittorrent_torrent[#{node['bittorrent']['torrent']}]", :immediately

Update the /etc/hosts file

The following example shows how the ruby_block resource can be used to update the /etc/hosts file:

ruby_block 'edit etc hosts' do
  block do
    rc ='/etc/hosts')
    rc.search_file_replace_line(/^127\.0\.0\.1 localhost$/,
      ' #{new_fqdn} #{new_hostname} localhost')

Set environment variables

The following example shows how to use variables within a Ruby block to set environment variables using rbenv.

node.override[:rbenv][:root] = rbenv_root
node.override[:ruby_build][:bin_path] = rbenv_binary_path

ruby_block 'initialize' do
  block do
    ENV['RBENV_ROOT'] = node[:rbenv][:root]
    ENV['PATH'] = "#{node[:rbenv][:root]}/bin:#{node[:ruby_build][:bin_path]}:#{ENV['PATH']}"

Call methods in a gem

The following example shows how to call methods in gems not shipped in Chef Infra Client

chef_gem 'mongodb'

ruby_block 'config_replicaset' do
  block do
    MongoDB.configure_replicaset(node, replicaset_name, rs_nodes)
  action :run
Edit this page on GitHub

Thank you for your feedback!


Search Results