Skip to end of metadata
Go to start of metadata


Subcommands for managing components from the Knife command line interface


This page contains documentation for each of the built-in knife sub-commands as well as popular plugins.

Many sub-commands are documented on their own pages, though all are listed here for completeness.

Common Options provides detail on the options can be passed to any subcommand, while sub-command specific options are reviewed in the relevant section of this page. Output Formats information on the amount of content displayed and modification options for the output.

For all commands and subcommands, there is Contextual Help for the options available, including direct access to manpages.

Knife sub-commands are structured such that commands have the form: NOUN verb NOUN (options).

Because the Chef Server API is RESTful, the verb of a knife command is generally a CRUD operation:

  • create (create)
  • list and show (read)
  • edit (update)
  • delete (destroy)



Built In Subcommands


Bootstrap

The Knife Bootstrap page explains the bootstrap subcommand. For Chef versions 0.10 and greater, the Knife Windows Bootstrap page describes the knife-windows plugin, which allows one to bootstrap Windows nodes.

return to top of page


Client

The Managing API Clients With Knife page explains the client subcommand used to manage API clients.

return to top of page


Cloud Plugins

Using the cloud provider subcommands is explained on the Launch Cloud Instances with Knife page.

return to top of page


Configure

The configure sub-commands provides an easy way to create knife.rb and client.rb files that can then be distributed to workstations and nodes.

return to top of page

There are two configure commands:

configure

Create a configuration file for knife. This will prompt for values to enter into the file. Default values are listed in square brackets if no other entry is typed.

Usage:

Additional Options:

  • -i, --initial:
    Create an initial API Client
  • -r, --repository REPO:
    The path to your chef-repo

Use -i if you would like to create an admin client (Hosted Chef users should note that creating a client as an admin does not change its permissions.)

configure client

Read the knife.rb config file and generate a config file suitable for use in /etc/chef/client.rb and copy the validation certificate into the specified directory.

Usage:


Cookbook

Managing Cookbooks With Knife provides detail on the use of this command to interact with cookbooks on your Chef Server.

return to top of page


Cookbook Site

Managing Cookbooks With Knife provides detail on the use of this command to interact with cookbooks on the Opscode Community Site.

return to top of page


Data Bag

Data bags are stores of arbitrary JSON data that and indexed and available via search. Managing Data Bags With Knife provides detail on the use of this command for managing data bags and data bag items.

return to top of page


Environment

The environment sub-command allows you to manage environments. See Managing Environments With Knife for use of the environments subcommand.

return to top of page


Exec

knife exec allows you to run Ruby scripts in a context similar to that of Shef. See Knife Exec for documentation on this command.

return to top of page


Node

See Managing Nodes With Knife on use of the node command to modify the Nodes supported by the Chef Server.

return to top of page


Recipe

Recipes on the Chef Server can be listed using the recipe list sub-command.

return to top of page

Specify PATTERN as a regular expression to limit the results.


Role

The role sub-commands allow you to manage roles saved on the Chef server and are structured similarly to the other knife sub-commands. See Managing Roles With Knife for details on its use.

return to top of page


Search

Search is one of the most useful features of Chef Server.

return to top of page

Search query syntax is covered in greater depth in Search. The following is the basic structure of the knife search command as well as some useful examples.

Usage:

Additional Options:

  • -a, --attribute ATTR:
    Show only one attribute
  • -i, --id-only:
    Show only the ID of matching objects
  • -q, --query QUERY:
    The search query; useful to protect queries starting with -
  • -R, --rows INT:
    The number of rows to return
  • -r, --run-list:
    Show only the run list
  • -o, --sort SORT:
    The order to sort the results in
  • -b, --start ROW:
    The row to start returning results at
  • -m, --medium:
    Display medium sized output when searching nodes using the default
    summary format
  • -l, --long:
    Display long output when searching nodes using the default summary
    format

Examples:

Search for the EC2 instance type (flavor) of all nodes running in EC2

Search for all nodes running Ubuntu:

Search for all nodes running CentOS in the production environment.


SSH

The SSH subcommand allows you to invoke commands in parallel on a subset of the nodes in your infrastructure.

return to top of page

Usage

The QUERY part of the command uses the same syntax as the search subcommand. This will return a list of servers that it will ssh into and run the COMMAND you've listed.

Additional Options:

  • -a, --attribute ATTR:
    The attribute to use for opening the connection - default is fqdn
  • -C, --concurrency NUM:
    The number of concurrent connections
  • -m, --manual-list:
    QUERY is a space separated list of servers
  • -P, --ssh-password PASSWORD:
    The ssh password
  • -x, --ssh-user USERNAME:
    The ssh username
  • -i, --identity-file IDENTITY_FILE:
    The SSH identity file used for authentication
  • -p, --ssh-port PORT:
    The ssh port
  • --no-host-key-verify:
    Disable host key verification

Examples:

Here's a simple example of this command:

This would ssh into all nodes that have the role "webserver" and run the command "sudo chef-client".

You could also use this to upgrade all of your nodes:

The SSH Subcommand could also be used to find the uptime of all your web servers:

Force a Chef run on each node returned by the search query:

Note the -x option tells knife to invoke ssh as the "ubuntu" user. The -a option tells knife to use the "ec2.public_hostname" attribute for opening the connection - default is fqdn.

Interpolation Rules for the SSH Subcommand

Because knife runs at a command prompt, it is important to be clear about interpolation rules for the SSH subcommand.

For example: assume that the nodes in a db role have a schema that is the same as the node, and that said schema contains a table named "dollars".

The command below will be interpolated in the context of the local machine. That is to say the $(hostname) will be the name of the machine from which knife was invoked. This is to be expected because the local shell sees double quotes and makes the substitution before the command is sent to the node(s). A mysql error from each node should be expected in this case.

Now consider the next command. It will be interpolated in the context of the node. That is to say the $(hostname) will be that of the node on which the command is run for each node in the db role. This is to be expected because the local shell sees single quotes and takes the string as a literal. The command is then interpolated by the each node's shell. A count of the number of rows in the dollars table from each node should be expected in this case.

The lesson here is to keep the interpolation rules of your local shell in mind when passing a command to your nodes via knife ssh.

Node Environment Customization Considerations

The interpolation rules for the SSH command can cause the behavior of knife ssh to appear erratic when custom configurations are used on the node. To understand how this can happen, consider the following scenario:

A user named "fred" places a .my.cnf file in his home directory on all of his nodes. Below is a template for this.

The .my.cnf file is read when a mysql client is invoked and substitutes the username, password and database behind the scenes so they do not need to be typed.

If, on his local machine, Fred invokes knife like this:

The result in this case will still be a count of rows in the dollars table for each node. In other words, it appears that interpolation takes place in the context of the node rather than locally.

However, what actually happened is the command mysql -e 'select count(*) from dollars' was sent to the node and the mysql client read the coincidentally correct database name from the .my.cnf file. Note that there are curly braces around the hostname, not parenthesis, so ${hostname} will evaluate to an empty string. It becomes quickly apparent that setting a different database name in the .my.cnf table from that of the node in this case could lead to even greater confusion when mysql returns saying the table does not exist in that schema. Or, if another user issues the same knife command and has no .my.cnf set, they will get an authentication error from mysql.

The lesson here is to be aware of any personal and global customizations because they can cause apparently erratic behavior that is, in fact, exactly correct.

Ensuring Process Runs Survive SSH Session

When a node script is run to start a background process, the script can complete and exit correctly, however the service that was to be run in the background had not completed prior to script closure. This appears most often with custom start-up scripts in /etc/init.d. These apparent inconsistencies are a product of how ssh handles terminal connections remotely, not issues with the knife-ssh subcommand.

Consider a custom server process called spaminatord, that connects to various university websites and gathers student information. If the start up script contains the below line to start spaminatord, it is at risk for exhibiting this behavior.

To insure the process runs beyond the life of the ssh session, the following need to be added to the command starting the process.

It is important to note that nohup and the redirection rules are all necessary to insure a clean start.

Alternate SSH Modes

The SSH subcommand supports a few alternate modes.

Interactive Mode

start an interactive ssh session. Commands will run agains all nodes (or a subset) returned by search query.

terminal multiplexer support - launch a tmux (or gnu screen) session with a window for each node returned by the search query:

tmux

Chef 10.6 and tmux
Typically, sessions are given names that match the search term used. Chef 0.10.6+ replaces any occurrences of ':' with '=' when generating the session name, since ':' is an illegal character for tmux session names.

GNU Screen

csshX

key-based authentication
Using csshX requires that you can authenticate to your nodes using key-based authentication and that your key has been added to your ssh-agent.

You can launch concurrent ssh sessions using csshX:

This will open a new terminal window for each machine as well as a master terminal which can be used to send commands to all of the nodes.

Mac Terminal

launch a new terminal window with a separate tab (and ssh session) for each node returned by the search query.

This sub-command requires extra gems!
The ssh macterm sub-command interact with mac terminal via applescript. You will need to install the rb-appscript gem on your local management workstation where you use knife.


Status

The status}] sub-command searches the Chef Server for all nodes and displays information about the last time the node checked into the server and executed a {{node.save.

return to top of page

Usage:

Specifying a QUERY allows you to restrict the results to a subset of the nodes.

Additional Options:

  • -r, --run-list RUN_LIST:
    Show the run list

Examples:

Show the status of all nodes in your organization

Include the nodes' run list in the status

show the status of a subset of nodes returned by a search query


Tag

Apply tags to nodes on a Chef Server.

return to top of page

create

Adds one or more tags to node.

delete

Removes one or more tags from node.

list

Lists the tags applied to node.

See also Managing Nodes with Knife


Extras


Bash command completion

Additional convenience may be provided using bash command completion.

return to top of page

Download the script and either source it in .bashrc or drop it in /etc/bash_completion.d/.

The completion employs optional caching if the directory $CHEF_HOME/.completion_cache exists and is writable (CHEF_HOME is ~/.chef by default). To clear the cache (e.g. because new roles or nodes are available) erase the files in the cache directory, they will be regenerated on the next completion run.

Note for OSX users: The script requires GNU sed to work, install gsed with macports or gnu-sed with Homebrew. You will also need the bash-completion script library if you do not already have it installed – a package exists named as such in macports and Homebrew (e.g. brew install bash-completion). Use the etc/bash_completion.d prefix appropriate for your environment.





Knife


Knife Bootstrap



Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.