CSC/ECE 517 Spring 2015/ch1a 2 WA
Knife
Introduction
Background
Knife is the command line tool for managing Chef nodes. If you are unfamiliar with Chef and how it works, check out the comparison between Chef and Puppet here: [1] or take a look at the official Chef website here: [2]. There, a deeper understanding of Chef can be attained. Simply, Chef allows the distribution of server environments between many different servers (called nodes). Any changes to the primary chef server (called the chef repo are distributed throughout all the other nodes, while different nodes can have other recipes and send them back to the chef repo. Knife, then, handles the communication between nodes and the chef repo. For example, let's say that there is an object on the chef repo that a node desires. Knife provides the tools to download that object to the node. Knife also allows setting up a node, installing necessary packages, management of users, and much more.
Examples
Using Knife is fairly straightforward, with usage following this syntax:
knife [verb] [object] [options]
The different verbs, or subcommands, for Knife are as follows, from the Using Knife page [3]: bootstrap, client, configure, cookbook, cookbook site, data bag, delete, deps, diff, download, edit, environment, exec, index rebuild, list, node, recipe list, role, search, show, ssh, status, tag, upload, user, and xargs.
Warning: Before you run many of these commands, you should have the knife editor set correctly. In the chef-repo, there are multiple ruby files that set the configuration of the environment. Inside of knife.rb, you need to add or set the following line.
<syntaxhighlight lang="ruby"> knife[:editor] = "/usr/bin/vim" </syntaxhighlight>
The path can be set to the text editor of choice, the above command sets the editor to vim.
The example we will use is also one that is useful for setting up nodes, and that is the usage of the bootstrap command. To set up a node, go to the command line on the chef repo and execute the following command:
knife bootstrap ip_address_of_node -x username -P password --sudo
First, the verb used is bootstrap. Bootstrap is the subcommand that allows the installation of a chef client on to a targeted node. Second, the object is the ip_address_of_node. Every subcommand takes an object, and since chef operates on many ruby principles and scripts, the object can be most anything. For boostrap, this object is the ip of the server address. For other commands, this might be something different. Third, we have the options for the bootstrap command and the object. For bootstrap, this includes the username and password for the node, and the option --sudo, to make sure all commands are executed correctly.
Before we do the next example, let's cover an important part of object strings and how they are treated by knife and the chef repo.
Wildcards A wildcard search can be used similar to standard regex commands, with one notable difference. The wildcard character itself must be escaped using the \ character. Here is an example from the "Using Knife" page on the chef website. Let's say the following was used to search for an object:
data_bags/a\*
Will return all of the objects that start with data_bags\a on the chef-node. However, if we were to run the following:
data_bags/a*
Will only search for objects on the chef repo corresponding to objects starting with data_bags/a on the node. Therefore, the * was applied before being sent to the chef repo, instead of after. So, as an example, let us list all of the data bags that are on the server that start with 'a'. Our object, then, is the string above, data_bags/a\*
. The verb that lists different objects on the chef repo is called list
. Our command we would run would be this:
knife list data_bags/a\*
Which would print to the console all of the objects in the data_bags directory that started with a.
References
1.Chef Documents About Knife 2.Chef Documents Knife Reference 3.