Running the World with Fabric

Fabric is a library and command-line application implemented in Python, designed to automate systems management tasks and

Fabric is a library and command-line application implemented in Python, designed to automate systems management tasks and deployment of applications. In addition to performing these tasks locally, Fabric also allows them to be carried out remotely, via SSH.

Fabric helps the user complete a variety of tasks, which can be condensed to these three:

  • Execute remote commands through ssh
  • Download and upload files to and from remote machines
  • Ask the user data

Cases of Use

  • Systematize the ´deploy´ process in an application (it may also not be a Python application)
  • Automate common systems management tasks:
    • installing applications
    • configuration changes
    • creating users
    • etc , etc , etc.


If we have pip installed on our system, we can install Fabric running:

Another option is to use the package administrator that corresponds to our system. For example in ubuntu:

Note: In some systems the package ´fabric´ can appear with the name python -fabric


The two main components of Fabric are the and the utility of the line of command fab.

The file is a Python module that contains the definition of the tasks that we will run. The fab command will be used to perform these tasks.
For example, a simple ¨hello world¨

We can do this as follows:

In this case we are running the task greeting passing in the parameter name the value Santiago.

Remote Command Execution

What makes Fabric so powerful is its excellent integration with SSH, which allows us to execute remote commands simply and clearly.
Fabric provides us with a set of basic operations described below for this. To test these transactions start a server configuration using Vagrant that is enclosed.

Is used to run a remote command. In the following example we can see how information from one system
From fabric.api from import run, task

We can perform this task on a remote server, specifying the ip and user who will run the commands

Note : When you run the above command, we will be asked to enter the password of the user vagrant . As shown, the execution of the command allows interaction between the user who launched it and the remote server.


Similar to run but can run the command with Super User permission.

The above command will create the directory with the name that you specify in the path / var / www /, giving a warning if it already exists and then list the files. As in the previous case can run with :

Note: If the previous script we used run instead of the command sudo, it would fail to create the directory and do not have permissions to write to / var / www .


It’s used to run a command on the local machine.

In the example we are listing the files in the current directory and later we’re compressing it. The lcd command allows us to move between directories locally.

This command lets you copy a file from your local machine to the remote machine

In this case, we create a task that allows us to upload the file to compress in the previous step to our remote server.


It’s used to download a file from the remote machine.

This task allows you to download apache logs and save them to a local folder.

If file with that name already exists in the local folder, the file will be overwritten.

Consult the user (the one running the Fabric script), to enter information required by the script

The previous task allows us to enter the name of the folder where you install our application and the port on which it will run.

It’s used to restart the remote system

Other Functions and Utilities

Some more things that Fabric provides:
Context Managers:
To define certain contexts in which a command is executed. For example, we can define environment variables, move to a directory, avoid execution errors, etc.
File management:
Provides a set of functions for managing remote files. Among other things it allows:

  • Checking whether a file exists
  • Comment or uncomment part thereof
  • Add information at the end or replace it
  • Upload a file from a template

How we utilize it

Fabric was used in the FOP – ARSAT project to create installation scripts and update our application. Using this library, we developed a script that allowed the customer to have the application installed and configured on his own servers in a matter of minutes with minimal configuration.
Additionally, we also use fabric to define common and recurring tasks within the project, for example:

  • Packaging the application
  • Deploying on continuous integration servers
  • Deploying on Test servers

Useful Links

Share this articleShare on LinkedInTweet about this on TwitterShare on FacebookShare on Google+Email this to someone
Go Back