Fabric: python library for devops tasks

Petr Nuzhnov, Five9

Why we need common system configuration managment (devops) tools?

stage #1 "God Mode"

stage #2 "Hell's tooling"

stage #3 'LITTLE CRASH'


Python library and command-line tool for streamining the use of SSH for application deployment or systems administration tasks

Jeff Forcier


  • paramiko
  • pyinvoke



pip install fabric


  • ecdsa
  • paramiko
  • pycrypto



from fabric.api import run

def list_directory():
    run('ls -la')
MacBook-Pro-Petr:fabr petrnuzhnov$ fab -H hostname.server.com list_directory -u username -p password
[hostname.server.com] Executing task 'list_directory'
[hostname.server.com] run: ls -la
[hostname.server.com] out: total 483168
[hostname.server.com] out: drwxr-xr-x. 41 username username      4096 Jun 16 07:22 .
[hostname.server.com] out: drwxr-xr-x.  4 root     root          4096 Dec 18  2014 ..
[hostname.server.com] out: -rw-------   1 username username     50498 Jun 16 10:00 .bash_history
[hostname.server.com] out: -rw-r--r--.  1 username username        18 Oct 16  2014 .bash_logout
[hostname.server.com] out: -rw-r--r--   1 username username       269 Jul 15  2015 .bash_profile
[hostname.server.com] out: -rw-r--r--   1 username username       174 Apr 15  2015 .bashrc
[hostname.server.com] out: drwxr-xr-x   2 username username      4096 Feb 27  2015 bin
[hostname.server.com] out: drwxr-xr-x   2 username username      4096 Oct  5  2015 Builds
[hostname.server.com] out: -rw-r--r--   1 username username    118135 May 11 03:08 temp.my
[hostname.server.com] out: drwxr-xr-x   3 username username      4096 May 10 03:25 .vim
[hostname.server.com] out: -rw-------   1 username username     13406 Jun 15 13:32 .viminfo
[hostname.server.com] out: -rw-r--r--   1 username username        90 Oct 29  2014 .vimrc

Main components

  • fab - command line utility
  • fabfile - file with described tasks
  • env - singleton dict subclass, which contains all necessary data: hosts, roles, credentials and other userfull settings
  • task - separate execution unit (e.g. function)


fab options and arguments

  • -f  - execution file
  • -H  - list of hosts
  • first_task second_task - tasks which will be executed one by one
  • - u and -p - credentials

task:value passing parameters to your task

fab -f my_task_list.py -H hosts first_task second_task:param1, param2 -u user -p passw



Configuration dict which is shared between tasks

  • env.hosts - list of hosts which will be involved
  • env.user
  • env.password
  • env.key_filename - path to ssh private key path
  • env.timeout - default timeout
  • env.keepalive=1 (default=0)



Hosts definition

env.roledefs['web'] = ['host1', 'host2',..]
env.roledefs['db'] = ['host3', 'host4']
env.roledefs = {'web':{'hosts':['host1', 'host2',..],

                'db':{'hosts':['host2', 'host3',...],
env.hosts = ['host1', 'host2', ...]




Simple env wrapper

def task():
    with settings(warn_only=True):
        #do something dangerous
def task():
    with settings(timeout=30):
        sudo('service mysqld restart')
def task():
    with settings(sudo_user=petrnuzhnov):
        run('cat ~/my_personal_file.txt')

Tasks defenition

# fabfile.py
def task1():
    """Task1 docstring"""

def task2():
    """Task2 docstring"""

def task_combined():
    """Task_combined docstring"""

def _not_a_task()
    print "I'm not a task function"
fab --list
Available commands:
    task1            Task1 docstring
    task2            Task2 docstring
    combined_task    Task_combined docstring

Task definition #2

# fabfile.py
from fabric.api import task, roles, run, env

def stop_web():
    sudo('service nginx stop')

def stop_db():
    sudo('service mysqld stop')

def stop_all():

def not_a_task()
    print "I'm not a task function"
fab --list
Available commands:


BUILT-In Operations

  • run
  • local
  • open_shell
  • require
  • reboot
  • prompt
  • sudo
  • cd
  • lcd
  • get
  • put

Execution (parallel and serial) on hosts

fab -H host1, host2, host3, host4, host5 parallel_task serial_task

def parallel_task():
    # task will be started on hosts in parallel

def serial_task():
    # will be in serial
fab -H host1, host2, host3, host4, host5 -P -z 5 parallel_task serial_task

# fabfile.py

def parallel_task():
    # task will be started on hosts in parallel

def serial_task():
    # will be in serial



  • python-user-friendly
  • no client side
  • flexible
  • easy to understand what is going on by reading of script


  • python 2.x
  • no parallel tasks under windows
  • not a silver bullet

Thank you!

Questions ?