7
June
2013

Comments Off | 0 Shares | | Tags: Devops Python-Fabric

As a DevOps guy working in a complex distributed systems project implemented in Agile we are constantly looking for innovative yet simple,elegant and robust solutions.We desperately needed a solution which would help us in Deployment,diagnosis of an issue,gather statistics or just the need to run scripts.

Fabric allows us to do all of the above and much much more.
It is a powerful automation tool which eliminates redundancy and covers huge volumes of operations need to be done daily. It is written in Python and hence felt all the more dearer.

Fabric is a Python library and command-line tool for streamlining the use of SSH. You could use it typically by creating a python module containing functions then execute them via the fab command line tool. Lets install and have some fun….

Installation is amazingly simple in any of the below modes

$sudo  pip install fabric

$sudo easy_install fabric

$ sudo apt-get install python-fabric

Please read the official documentation for dependancies
Post installation you will need to create your fabfile.py in which you will define the functions/tasks you will like to perform on the remote servers.Fabfile is the place where all of the roles,configurations will be defined.Its all in python so you have the flexibility of simple ‘one-script-for-all’ to complex class based implementation.
Lets see a simple example.

1
2
3
4
5
6
7
8
9
10
from fabric.api import env, run
 
def servers():
    env.hosts = ['SERVERA','SERVERB']
    env.user = '<remote-system-user>'
    env.key_filename = ['<absolute/path/to/key.pem>']
    env.skip_bad_hosts = True
 
def restart_apache():
    run('sudo /etc/init.d/apache2 restart')
from fabric.api import env, run

def servers():
    env.hosts = ['SERVERA','SERVERB']
    env.user = '<remote-system-user>'
    env.key_filename = ['<absolute/path/to/key.pem>']
    env.skip_bad_hosts = True

def restart_apache():
    run('sudo /etc/init.d/apache2 restart')

env.hosts is a list so have your servers[IP-Address] as a list element
env.user you can run the tasks with different users
env.key_filename your ssh authentication key

Now go to the fabfile.py directory and execute as below

$ fab servers restart_apache –run the restart apache task on the list of servers .
which results in
[SERVERA] Executing task ‘restart_apache’
[SERVERB] Executing task ‘restart_apache’

Disconnecting from [SERVERA]… done.
Disconnecting from [SERVERB]… done.

There are plenty of env options to choose from as per our need.
eg. To check the return code of your command
set the env.warn_only=True and check the return_code attribute of object returned from run().
If you add env.warn_only = True in servers() then in restart_apache() we can do
output = run(‘sudo /etc/init.d/apache2 restart’)
then check for output.failed which returns (True/False)
or
output.return_code which returns the actual return code of the command.

Fabric is definitely a ‘must-have tool’ in the DevOps tool-kit.

Follow on Google+ LinkedIn Updated on December 26, 2013 at