Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  KNIFE-EXEC (1)


knife-exec - The man page for the knife exec subcommand. \$1 \n[an-margin] level \n[rst2man-indent-level] level margin: \n[rst2man-indent\n[rst2man-indent-level]] - \n[rst2man-indent0] \n[rst2man-indent1] \n[rst2man-indent2]

The knife exec subcommand uses the Knife configuration file to execute Ruby scripts in the context of a fully configured chef-client. This subcommand is most often used to run scripts that will only access Chef server one time (or otherwise very infrequently). Use this subcommand any time that an operation does not warrant full usage of the Knife subcommand library.

Authenticated API Requests

The knife exec subcommand can be used to make authenticated API requests to the Chef server using the following methods:

Method Description
api.delete Use to delete an object from the Chef server.
api.get Use to get the details of an object on the Chef server. Use to add an object to the Chef server.
api.put Use to update an object on the Chef server.

These methods are used with the -E option, which executes that string locally on the workstation using chef-shell. These methods have the following syntax:

$ knife exec -E \(aqapi.method(/endpoint)\(aq

o api.method is the corresponding authentication method --- api.delete, api.get,, or api.put
o /endpoint is an endpoint in the Chef Server API

For example, to get the data for a node named "Example_Node":

$ knife exec -E \(aqputs api.get("/nodes/Example_Node")\(aq

and to ensure that the output is visible in the console, add the puts in front of the API authorization request:

$ knife exec -E \(aqputs api.get("/nodes/Example_Node")\(aq

where puts is the shorter version of the $stdout.puts predefined variable in Ruby.

The following example shows how to add a client named "IBM305RAMAC" and the /clients endpoint, and then return the private key for that user in the console:

$ client_desc = {
    "name"  => "IBM305RAMAC",
    "admin" => false

new_client ="/clients", client_desc) puts new_client["private_key"]


This subcommand has the following syntax:

$ knife exec SCRIPT (options)


This subcommand has the following options:
  The configuration file to use.
--chef-zero-port PORT
  The port on which chef-zero will listen.
  Use to view colored output.
-d, --disable-editing
  Use to prevent the $EDITOR from being opened and to accept data as-is.
  Use to have Knife use the default value instead of asking a user to provide one.
-E CODE, --exec CODE
  A string of code that will be executed.
-e EDITOR, --editor EDITOR
  The $EDITOR that is used for all interactive commands.
--environment ENVIRONMENT
  The name of the environment. When this option is added to a command, the command will run only against the named environment.
-F FORMAT, --format FORMAT
  The output format: summary (default), text, json, yaml, and pp.
-h, --help Shows help for the command.
-k KEY, --key KEY
  The private key that Knife will use to sign requests made by the API client to the Chef server.
-p PATH:PATH, --script-path PATH:PATH
  A colon-separated path at which Ruby scripts are located.
  Use to show data after a destructive operation.
-s URL, --server-url URL
  The URL for the Chef server.
-u USER, --user USER
  The user name used by Knife to sign requests made by the API client to the Chef server. Authentication will fail if the user name does not match the private key.
-v, --version
  The version of the chef-client.
-V, --verbose
  Set for more verbose outputs. Use -VV for maximum verbosity.
-y, --yes Use to respond to all confirmation prompts with "Yes". Knife will not ask for confirmation.
-z, --local-mode
  Use to run the chef-client in local mode. This allows all commands that work against the Chef server to also work against the local chef-repo.


There are three ways to use knife exec to run Ruby script files. For example:

$ knife exec /path/to/script_file


$ knife exec -E \(aqRUBY CODE\(aq


$ knife exec

To check the status of Knife using a Ruby script named "status.rb" (which looks like):

printf "%-5s %-12s %-8s %s\n", "Check In", "Name", "Ruby", "Recipes"
nodes.all do |n|
   checkin =[\(aqohai_time\(aq]).strftime("%F %R")
   rubyver = n[\(aqlanguages\(aq][\(aqruby\(aq][\(aqversion\(aq]
   recipes = n.run_list.expand(_default).recipes.join(", ")
   printf "%-20s %-12s %-8s %s\n", checkin,, rubyver, recipes

and is located in a directory named "scripts", enter:

$ knife exec scripts/status.rb

To show the available free memory for all nodes, enter:

$ knife exec -E \(aqnodes.all {|n| puts "#{} has #{} free memory"}\(aq

To list all of the available search indexes, enter:

$ knife exec -E \(aqputs api.get("search").keys\(aq

To query a node for multiple attributes using a Ruby script named search_attributes.rb (which looks like):

% cat scripts/search_attributes.rb
query = ARGV[2]
attributes = ARGV[3].split(",")
puts "Your query: #{query}"
puts "Your attributes: #{attributes.join(" ")}"
results = {}
search(:node, query) do |n|
   results[] = {}
   attributes.each {|a| results[][a] = n[a]}

puts results exit 0


% knife exec scripts/search_attributes.rb "hostname:test_system" ipaddress,fqdn

to return something like:

Your query: hostname:test_system
Your attributes: ipaddress fqdn
{""=>{"ipaddress"=>"", "fqdn"=>""}}




Search for    or go to Top of page |  Section 1 |  Main Index

KNIFE-EXEC (1) Chef 11.14

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.