DevOps: Introduction to Fabric.

In this post, we will introduce Fabric.

Fabric is a simple, yet powerful, tool for remotely controlling a farm of servers from your terminal.

As a warmup example, let’s assume that you want to check the uptime of a list of servers, say server1.example.com, server2.example.com, …, serverN.example.com.

Of course, you can always write a bash script as the one below to do so,

#!/bin/bash
HOSTS=server1.example.com,server2.example.com,...,serverN.example.com

for HOST in HOSTS
do
    ssh -i ~/.ssh/example_rsa @{USER}@HOST -s 'uptime'
done

A curious reader might ask why to bother using fabric or any other tool for the above operation?

An answer to this question is that fabric focuses on what are the operations to be performed on the servers whereas the above script has to be also concerned of how the tasks will be executed. For example, how would you run the above for loop in parallel? Using fabric is quite simple, see parallel fabric for more details.

Next, we describe how to install fabric and then how to configure it for the above uptime task.

Installing Fabric

To install fabric, you first need to ensure that you have Python installed, and also install python pip. Then, to instal fabric simply do

sudo pip install fabric

Configuring Fabric

Configure fabric is straightforward. Simply create a fabfile.py in you current directory with the following content. Also create a server.txt with its i-th line equal to server{i}.example.com.

from fabric.api import env
from fabric.operations import run, put, sudo,local

# Setup username and its RSA key
env.user  = 'ubuntu'
env.key_filename = '~/.ssh/example_rsa'

# Populate host names 
with open('servers.txt') as f:
    env.hosts = f.readlines()

def uptime():
    run('uptime')

Running Fabric

To run fabric, simply do

fab uptime

To execute fabric in parallel, you can simple do

fab uptime -P

See parallel fabric for more details on parallel execution.

Written on March 1, 2015