Skip to main content

Chef Infra Language: Cookbook Execution

[edit on GitHub]

The Chef Infra Language includes helper methods for gathering information on the execution of the Chef Infra Client recipe and resource code. This information can be used in recipes and resources to take specific actions.

Chef Infra Client State

These helpers allow you to understand the state of the node that Chef Infra Client is executing on.


Use the node method, often referred to as the node object, to access data collected on the system through Ohai as well as node attributes set in cookbooks or Policyfiles.

The syntax for the cookbook_name method is as follows:



Use the cookbook_name method to return the name of a cookbook.

The syntax for the cookbook_name method is as follows:


This method is often used as part of a log entry. For example:'I am a message from the #{recipe_name} recipe in the #{cookbook_name} cookbook.')


Use the recipe_name method to return the name of a recipe.

The syntax for the recipe_name method is as follows:


This method is often used as part of a log entry. For example:'I am a message from the #{recipe_name} recipe in the #{cookbook_name} cookbook.')


Use the resources method to look up a resource in the resource collection. The resources method returns the value for the resource that it finds in the resource collection. The preferred syntax for the resources method is as follows:


but the following syntax can also be used:

resources(resource_type: 'resource_name')

where in either approach resource_type is the name of a resource and resource_name is the name of a resource that can be configured by Chef Infra Client.

The resources method can be used to modify a resource later on in a recipe. For example:

file '/etc/hosts' do
  content ' localhost.localdomain localhost'

and then later in the same recipe, or elsewhere:

f = resources('file[/etc/hosts]')
f.mode '0644'

where file is the type of resource, /etc/hosts is the name, and f.mode is used to set the mode property on the file resource.


Use the attribute? method to ensure that certain actions only execute in the presence of a particular node attribute. The attribute? method will return true if one of the listed node attributes matches a node attribute that is detected by Ohai during every Chef Infra Client run.

The syntax for the attribute? method is as follows:


For example:

if node.attribute?('ipaddress')
  # the node has an ipaddress


Use the reboot_pending? method to test if a node needs a reboot, or is expected to reboot. reboot_pending? returns true when the node needs a reboot.

The syntax for the reboot_pending? method is as follows:


Executing Code

These helpers allow you to include recipes and impact how resources run on the system.


A recipe can include one (or more) recipes from cookbooks by using the include_recipe method. When a recipe is included, the resources found in that recipe will be inserted (in the same exact order) at the point where the include_recipe keyword is located.

The syntax for including a recipe is like this:

include_recipe 'recipe'

For example:

include_recipe 'apache2::mod_ssl'

Multiple recipes can be included within a recipe. For example:

include_recipe 'cookbook::setup'
include_recipe 'cookbook::install'
include_recipe 'cookbook::configure'

If a specific recipe is included more than once with the include_recipe method or elsewhere in the run_list directly, only the first instance is processed and subsequent inclusions are ignored.


Use the with_run_context method to define a block that has a pointer to a location in the run_context hierarchy. Resources in recipes always run at the root of the run_context hierarchy, whereas custom resources and notification blocks always build a child run_context which contains their sub-resources.

The syntax for the with_run_context method is as follows:

with_run_context :type do
  # some arbitrary pure Ruby stuff goes here

where :type may be one of the following:

  • :root runs the block as part of the root run_context hierarchy
  • :parent runs the block as part of the parent process in the run_context hierarchy

For example:

action :run do
  with_run_context :root do
    edit_resource(:my_thing, "accumulated state") do
      action :nothing
      my_array_property << accumulate_some_stuff
  log "kick it off" do
    notifies :run, "my_thing[accumulated state]", :delayed

Was this page helpful?