Getting Started With the Fabric Python Library

, , Comments Off on Getting Started With the Fabric Python Library

Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. Fabric is very simple and powerful and can help to automate repetitive command-line tasks. This approach can save time by automating your entire workflow. 

This tutorial will cover how to use Fabric to integrate with SSH and automate tasks.

Installation

Fabric is best installed via pip:

pip install fabric

Getting Started With Fabric

Usage

Below is a simple function demonstrating how to use Fabric.

def welcome():
    print("Welcome to getting started with Fabric!")

The program above is then saved as fabfile.py in your current working directory. The welcome function can be executed with the fab tool as follows:

$ fab welcome
Welcome to getting started with Fabric

Fabric provides the fab command which reads its configuration from a file, fabfile.py. The file should be in the directory from which the command is run. A standard fabfile contains the functions to be executed on a remote host or a group of remote hosts.