Chef

About Knife¶

knife is a command-line tool that provides an interface between a local chef-repo and the Chef server. knife helps users to manage:

  • Nodes
  • Cookbooks and recipes
  • Roles
  • Stores of JSON data (data bags), including encrypted data
  • Environments
  • Cloud resources, including provisioning
  • The installation of the chef-client on management workstations
  • Searching of indexed data on the Chef server

Note

The Knife Quick Reference provides an all-in-one quick reference of knife commands. View a web-based PNG file here: http://docs.getchef.com/_images/qr_knife_web.png. Or download the source files from here: https://github.com/opscode/quick-reference. Print the front/back source files and laminate them for best effect.

Topic Description
Working with Knife knife runs from a management workstation and sits in-between a Chef server and an organization’s infrastructure.
Common Options There are many options that are available for all knife sub-commands.

Note

The knife executable cannot be run as a daemon.

knife includes the following sub-commands:

Sub-command Description
knife bootstrap The knife bootstrap subcommand is used to run a bootstrap operation that installs the chef-client on the target system. The bootstrap operation must specify the IP address or FQDN of the target system.
knife client The knife client subcommand is used to manage an API client list and their associated RSA public key-pairs. This allows authentication requests to be made to the Chef server by any entity that uses the Chef server API, such as the chef-client and knife.
knife configure The knife configure subcommand is used to create the knife.rb and client.rb files so that they can be distributed to workstations and nodes.
knife cookbook The knife cookbook subcommand is used to interact with cookbooks that are located on the Chef server or the local chef-repo.
knife cookbook site The knife cookbook site subcommand is used to interact with cookbooks that are located at https://supermarket.getchef.com/cookbooks. A user account is required for any community actions that write data to this site. The following arguments do not require a user account: download, search, install, and list.
knife data bag The knife data bag subcommand is used to manage arbitrary stores of globally available JSON data.
knife delete The knife delete subcommand is used to delete an object from a Chef server. This subcommand works similar to knife cookbook delete, knife data bag delete, knife environment delete, knife node delete, and knife role delete, but with a single verb (and a single action).
knife deps The knife deps subcommand is used to identify dependencies for a node, role, or cookbook.
knife diff The knife diff subcommand is used to compare the differences between files and directories on the Chef server and in the chef-repo. For example, to compare files on the Chef server prior to an uploading or downloading files using the knife download and knife upload subcommands, or to ensure that certain files in multiple production environments are the same. This subcommand is similar to the git diff command that can be used to diff what is in the chef-repo with what is synced to a git repository.
knife download The knife download subcommand is used to download roles, cookbooks, environments, nodes, and data bags from the Chef server to the current working directory. It can be used to back up data on the Chef server, inspect the state of one or more files, or to extract out-of-process changes users may have made to files on the Chef server, such as if a user made a change that bypassed version source control. This subcommand is often used in conjunction with knife diff, which can be used to see exactly what changes will be downloaded, and then knife upload, which does the opposite of knife download.
knife edit The knife edit subcommand is used to edit objects on the Chef server. This subcommand works similar to knife cookbook edit, knife data bag edit, knife environment edit, knife node edit, and knife role edit, but with a single verb (and a single action).
knife environment The knife environment subcommand is used to manage environments within a single organization on the Chef server.
knife exec 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.
knife index rebuild The knife index rebuild subcommand is used to rebuild the search indexes for the open source Chef server. This operation is destructive and may take some time.
knife list The knife list subcommand is used to view a list of objects on the Chef server. This subcommand works similar to knife cookbook list, knife data bag list, knife environment list, knife node list, and knife role list, but with a single verb (and a single action).
knife node The knife node subcommand is used to manage the nodes that exist on a Chef server.
knife raw The knife raw subcommand is used to send a REST request to an endpoint in the Chef server API.
knife recipe list The knife recipe list subcommand is used to view all of the recipes that are on a Chef server. A regular expression can be used to limit the results to recipes that match a specific pattern. The regular expression must be within quotes and not be surrounded by forward slashes (/).
knife role The knife role subcommand is used to manage the roles that are associated with one or more nodes on a Chef server.
knife search The knife search subcommand is used run a search query for information that is indexed on a Chef server.
knife serve The knife serve subcommand is used to run a persistent chef-zero against the local chef-repo. (chef-zero is a lightweight Chef server that runs in-memory on the local machine.) This is the same as running the chef-client executable with the --local-mode option. The chef_repo_path is located automatically and the Chef server will bind to the first available port between 8889 and 9999. knife serve will print the URL for the local Chef server, so that it may be added to the knife.rb file.
knife show The knife show subcommand is used to view the details of one (or more) objects on the Chef server. This subcommand works similar to knife cookbook show, knife data bag show, knife environment show, knife node show, and knife role show, but with a single verb (and a single action).
knife ssl check

The knife ssl check subcommand is used to verify the SSL configuration for the Enterprise Chef and/or Open Source Chef servers, or at another location specified by a URL or URI.

Warning

When verification of a remote server’s SSL certificate is disabled, the chef-client will issue a warning similar to “SSL validation of HTTPS requests is disabled. HTTPS connections are still encrypted, but the chef-client is not able to detect forged replies or man-in-the-middle attacks.” To configure SSL for the chef-client, set ssl_verify_mode to :verify_peer (recommended) or verify_api_cert to true in the client.rb file.

knife ssl_fetch

The knife ssl fetch subcommand is used to copy SSL certificates from an HTTPS server to the trusted_certs_dir directory that is used by knife and the chef-client to store trusted SSL certificates. When these certificates match the hostname of the remote server, running knife ssl fetch is the only step required to verify a remote server that is accessed by either knife or the chef-client.

Warning

It is the user’s responsibility to verify the authenticity of every SSL certificate before downloading it to the trusted_certs_dir directory. knife will use any certificate in that directory as if it is a 100% trusted and authentic SSL certificate. knife will not be able to determine if any certificate in this directory has been tampered with, is forged, malicious, or otherwise harmful. Therefore it is essential that users take the proper steps before downloading certificates into this directory.

knife ssh The knife ssh subcommand is used to invoke SSH commands (in parallel) on a subset of nodes within an organization, based on the results of a search query made to the Chef server.
knife status The knife status subcommand is used to display a brief summary of the nodes on a Chef server, including the time of the most recent successful chef-client run.
knife tag The knife tag subcommand is used to apply tags to nodes on a Chef server.
knife upload The knife upload subcommand is used to upload roles, cookbooks, environments, and data bags to the Chef server from the current working directory in the chef-repo. This subcommand is often used in conjunction with knife diff, which can be used to see exactly what changes will be uploaded, and then knife download, which does the opposite of knife upload.
knife user The knife user subcommand is used to manage the list of users and their associated RSA public key-pairs.
knife xargs The knife xargs subcommand is used to take patterns from standard input, download as JSON, run a command against the downloaded JSON, and then upload any changes.