Using custom knife plugins
Knife comes with a set of commands out of the box. The built-in commands deal with the basic elements of Chef-like cookbooks, roles, data bags, and so on. However, it would be nice to use knife for more than just the basic stuff. Fortunately, knife comes with a plugin API and there are already a host of useful knife plugins built by the makers of Chef and the Chef community.
Getting ready
Make sure you have an account at Amazon Web Services (AWS) if you want to follow along and try out the knife-ec2
plugin. There are knife plugins available for most Cloud providers. Go through the There's more... section of this recipe for the list.
How to do it...
Let's see which knife plugins are available, and try to use one to manage Amazon EC2 instances:
- List the knife plugins that are shipped as Ruby gems using the
chef
command-line tool:mma@laptop:~/chef-repo $ chef gem search -r knife-
*** REMOTE GEMS *** ...TRUNCATED OUTPUT... knife-azure (1.3.0) ...TRUNCATED OUTPUT... knife-ec2 (0.10.0) ...TRUNCATED OUTPUT...
- Install the EC2 plugin to manage servers in the Amazon AWS Cloud:
mma@laptop:~/chef-repo $ chef gem install knife-ec2
Building native extensions. This could take a while... ...TRUNCATED OUTPUT... Fetching: knife-ec2-0.10.0.gem (100%) Successfully installed knife-ec2-0.10.0 ...TRUNCATED OUTPUT... 6 gems installed
- List all the available instance types in AWS using the
knife ec2
plugin. Please use your own AWS credentials instead ofXXX
andYYYYY
:mma@laptop:~/chef-repo $ knife ec2 flavor list --aws-access-key-id XXX --aws-secret-access-key YYYYY
ID Name Arch RAM Disk Cores c1.medium High-CPU Medium 32-bit 1740.8 350 GB 5 …TRUNCATED OUTPUT… m2.xlarge High-Memory Extra Large 64-bit 17510. 420 GB 6.5 t1.micro Micro Instance 0-bit 613 0 GB 2
How it works...
Knife looks for plugins at various places.
First, it looks into the .chef
directory, which is located inside your current Chef repository, to find the plugins specific to this repository:
./.chef/plugins/knife/
Then, it looks into the .chef
directory, which is located in your home directory, to find the plugins that you want to use in all your Chef repositories:
~/.chef/plugins/knife/
Finally, it looks for installed gems. Knife will load all the code from any chef/knife/
directory found in your installed Ruby gems. This is the most common way of using plugins developed by Chef or the Chef community.
There's more...
There are hundreds of knife plugins, including plugins for most of the major Cloud providers, as well as the major virtualization technologies, such as VMware, vSphere, and Openstack, amongst others.
See also
- To learn how to write your own knife plugins, see the Creating custom knife plugins recipe in Chapter 2, Evaluating and Troubleshooting Cookbooks and Chef Runs
- Find a list of supported Cloud providers at http://docs.chef.io/plugin_knife.html