Fabric: a System Administrator's Best Friend

The Basics

Now that you understand the groundwork of Fabric, you can start putting
it to use. For this article, I explain how to make a
simple fabfile for the purpose of installing/removing software and your
machines. First, you need what is called a fabfile. The fabfile
contains all of your Fabric functions. By default, it needs to be named
fabfile.py and be in the working directory, but as mentioned previously, you can
specify the fabfile from the command line if need be. So, open your fabfile
and start it with from fabric.api import * to include all the Fabric
functionality. Then define all of your functions. Let's start with installing
some software:

You then can install a package via yum on all of your machines by running:

$ fab --hosts=host1,host2,host3 install

Then, you'll be prompted for the package to install only once.
Alternatively, since you indicated an optional parameter of
pkg, you can
indicate that from the command line so you won't be prompted on execution,
like this:

$ fab --hosts=host1,host2,host3 install:pkg=wormux

or:

$ fab --hosts=host1,host2,host3 install:wormux

Also note that you are prompted for the password for both SSH
and sudo only once. Fabric stores this in memory and reuses it, if possible,
for every other machine. Congratulations! You've just successfully
created your first Fabric script. It's as simple as that!

Tips and Tricks

I've picked up some neat tricks since I've started with Fabric.
First, you generally never see a Fabric command as simple as what is
above. When fully automated, it looks more like this:

This is particularly convenient if you have a variety of fabfiles that you
use on different groups of machines, or in different contexts.

There are occasions when you need to execute certain commands from
within a specific directory. Because each command is a discrete and
non-persistent connection to the machine, this is not inherently simple.
However, simply by enclosing the necessary commands in a
with statement, you
have a solution:

More Information

There are several ways to get help with Fabric. The most effective is to
use the fab-file mailing list. The developers are
generally very prompt in responding. There is also a Fabric Twitter
account @pyfabric where Fabric news and
announcements are released. You can submit and view bugs through the
Fabric Github page. Of
course, you also can't discount the #fabric channel on Freenode, where you can
connect with the community and get some quick answers. Finally, you
always can browse the documentation hosted at http://www.fabfile.org.

I've started using 'Salt' for this purpose. It handles all the rsa key authentication automatically, allows for remote execution of scripts, and also, offers a higher level configuration management system.http://saltstack.com/community.html