Python Fabric

logo

Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

Links

Install

 yum install fabric

OR

 pip install fabric

Run

I plan on using Fabric as a push-to config management service which will work very nicely as a secondary piece to my CFEngine setup. I used this to deploy a security update to 120+ hosts in under 20 minutes.

Standalone

 ]# fab -H server001 -- yum -y update glibc # will ask for password

With fabfile

one can create a fabfile.py with default settings and expanded tasks to execute, also paralled task execution can be achived.

 ]# vim fabfile.py
 from fabric.api import *
 from fabric.contrib.files import *
 
 env.hosts = [
         'server001',
         'server002',
         '...',
 ]
 
 env.user = "root" #it runs as parallel operations so there is no chance to enter a password for sudo users
 env.password = ""
 
 @parallel
 def install():
         run("yum -y update glibc")

Then to run:

 ]# fab -P install -f fabfile.py

Advanced concepts

The following is more advanced options that can be included within a fabfile in order to achieve different results.

env.roledefs = {
      'testing': [
      'server001',
      'server002',
      ]
}
 ]# fab install:roles=testing
def disk_check():
        run("df -hl")

def ram_check():
        run("free -h")


def check_all():
        print("Executing on %s as %s" % (env.host, env.user))
        upload_template("/tmp/fabric_hello", "/var/tmp/fabric_hello")# source, destination
        execute(disk_check)
        execute(ram_check)
 ]# fab check_all:roles=testing 

will run all tasks in order on each machine, do not use parallel execution if you expect to use the results returned to screen in anyway as parallel stdout/stderr does not return in order.