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:


def install(pkg=None):
   if pkg is not None:
      env["pkg"] = pkg
   elif pkg is None and env.get("pkg") is None:
      env["pkg"] = prompt("Which package? ")
   sudo('yum install -y %s' % env["pkg"])

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:


$ fab --skip-bad-hosts -u user -p 12345 -i ~/.ssh/id_dsa --warn-only
 ↪--hosts=host1,host2,host3,host4,host5,host6,host7,host8,host9,host10
 ↪--parallel --pool-size=20 install:pkg=wormux

Who wants to type that out every time they want to run a command? No one! That's why aliasing almost all of that is so convenient and efficient. Add the following to your .bashrc file:


alias f="fab --skip-bad-hosts -u user -p 12345 -i ~/.ssh/id_dsa
 ↪--warn-only
 ↪--hosts=host1,host2,host3,host4,host5,host6,host7,host8,host9,host10
 ↪--parallel"

Then, all you have to do each time you want to run Fabric is this:


$ f install:pkg=wormux

Even using this technique, your alias can become cumbersome if you have more than a few machines you commonly administer. A simple solution to that is to add this function to your fabfile:


def set_hosts():
   env.hosts = open('hosts', 'r').readlines()

Then, put all your hostnames in a file called hosts in the same directory as your fabfile, and modify your alias to look like this:


alias f="fab --skip-bad-hosts -u user -p 12345 -i ~/.ssh/id_dsa
 ↪--warn-only --parallel set_hosts"

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:


with cd("~/gitrepo"):
   run('git add --all')
   run('git commit -m "My super awesome automated 
   ↪commit script for `date`"')

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.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Running arbitrary command trick

Athmane's picture

Fabric can be used to run arbitrary command with:

fab -u username -H hostname -- 'uname -a'

thanks

symbian's picture

iam enjoyed and like this

I usually just do something

jbowen7's picture

I usually just do something like the following:

for i in $SERVER1 $SERVER2 $SERVER3; do
scp myTasks.sh root@$i:/tmp
ssh root@$i '/bin/bash /tmp/myTasks.sh ; [[ $? != 0 ]] && echo "stuff broke" || rm -f /tmp/myTasks.sh'
done

Good content, I trust this is

Jiad's picture

Good content, I trust this is a good weblog about Wish to see refreshing content material next time. Thanks for sharing this publish with us. Keep it up.voyance gratuitement

A good alternative

Anonymous's picture

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

hello !!!

linda99's picture

Hi everyone your site is great, it is comprehensive and super attractive! your little presentation is very nice! Good luck on my part!
Avenir amour

Perl alternative

Anonymous's picture

There is a similar Perl alternative around, which I prefer for such tasks, as the Perl syntax is just simpler. It's called (R)?ex or just "Rex".

http://rexify.org/

Nice, thanks for the tip.

anti ddos's picture

Nice, thanks for the tip. Looks interesting.

Another Approach To This Problem

A. Coder's picture

I wrote something along these lines some time ago, but packaged it as a turnkey utility:

http://www.tundraware.com/Software/tsshbatch

Another Approach To The Same Problem

A, Coder's picture

Something I wrote along the same lines but more
as a turnkey utility:

http://www.tundraware.com/Software/tsshbatch

pssh!

JonnoN's picture

Been using pssh for quite a while to do just this!

http://code.google.com/p/parallel-ssh/

And since I'm using Bash to execute items, I can combine it with any arbitrary Bash code to make robust, complex, elegant applications for deployment or administration tasks. :)

Better tool available

Anonymous's picture

I have found CSSH (Cluster SSH) to do this task quite well. Plus it sounds a lot more simple to use.

?

Anonymous's picture

Hello there.

I believe that almost every linux admin has done something like that tool-set on his/hers career. Either with python, ruby, or (k,z,c,ba,)sh script...

But what I find good about this is that it's not being kept closed in a "box". :)

Good job, and thank you for doing this.

Rgds,

Me:)

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState