Data Flow:

What is Fabric?

  • Why We Use Fabric?
  • How To Use Fabric?
  • Example of how to install WordPress application and Deploy custom developed plugin

What is Fabric?

  • Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
  • It provides a basic suite of operations
    • Executing local or remote shell commands
    • Uploading/downloading files
    • Prompting the running user for input
    • Aborting execution.
  • It can be used to automate deployments, run single or multiple commands on multiple servers in parallel or pretty much anything else you can think of that involves remotely logging in to a server, copying files, etc.
  • User can execute shell commands over SSH, so you only need to have SSH running on the remote machine. It interacts with the remote machines that you specify as if they were local.

Why We Use Fabric?

  • It is a better way to mention script for deployment process of application and no matter who has deployed.
  • Most system administrators have “cheat sheets”, that they use to remember how to do a task that is frequently repeated. Most of these tasks involve using SSH to log in to a server and run a series of commands. Fabric allows to convert the repetitive task into code that can be run using SSH. So system administrators doesn’t make any typographical error in commands they are running, logging in to the wrong server accidentally or forgetting to do a critical step.

How To Use Fabric?

  • Installation

To install Fabric using pip, run following command,

sudo pip install fabric

Also, using Operating system’s package manager,

sudo apt-get install fabric


  • Now, we learn about some of the functions in Fabric. Fabric provides a set of commands in fabric.api module:
  1. run – Fabric run command is used for executing a shell command on one or more remote hosts.
  2. sudo – It allows the execution of a given set of commands and arguments with sudo privileges on the remote host.
  3. local – Fabric local command is used for executing shell commands locally.
  4. get – The get command exists to download (i.e. pull) file(s) from the remote system to the computer where the Fabric is being used.
  5. put – When a user needs to upload files, put command can be used very similarly to ‘get’ command.
  6. prompt – This command asks the user (i.e. one that is running the script) to input a certain data to use during the successive execution.
  7. reboot – It is used to reboot the remote system. Default waiting time is 2 mins before performing the task/job


  • After successful installation, it’s time to write your first Fabric file!

from fabric.api import *

env.user = ‘username’

def hello():

run(‘echo “hello from `hostname`”‘)

The ‘user’ is the user-name used to login remotely to the servers.


To run ‘hello’ function on remote machine use following command:

fab -H <host> hello


Example to run this script on ‘devopstech’ :

fab -H devopstechhello

Expected output:

[devopstech] Executing task ‘hello’

[devopstech] run: echo “hello from `hostname`”

[devopstech] Login password for ‘user’:

[devopstech] out: hello from

[devopstech] out:


Disconnecting from devopstech… done.


Install WordPress application and Deploy custom developed plugin

WordPress plugins allow users to easily modify, customize and enhance any WordPress website. Many developers create plugins to enhance a custom theme or to modularize their code. While developing a plugin, user always use the WordPress SCM repository (e.g git) to host a plugin code. So after the development of plugin user need to deploy it and test on the local environment.

Let’s install WordPress application and deploy plugin using Fabric

The following Fabric Code Performs accomplish steps:

  • Install WordPress on remote machine
    • Download user’s custom plugin code from git repo
    • Zip the deployable files before deploying the plugin
    • Copy zip file to remote server and unzip to WordPress Plugin directory
    • Start the service


from fabric.api import *

env.user = prompt(“SSH Username:”)

def wordpress():

dbuser = prompt(“WordPress Database Username: “)

dbpassword = prompt(“WordPress Database User Password: “)

dbname = prompt(“WordPress Database Name: “)

dbhost = prompt(“Database Host: “)

env.dir = ‘/var/www/html’

wpconfigfile = ‘%s/wordpress/wp-config.php’ % env.dir

#HTTP installation

if run(“ls -al /etc/httpd/conf/httpd.conf”, warn_only=True).succeeded:

run(‘echo “httpd already installed”‘)


run(‘yum install httpd php-fpm php php-devel php-mysql -y’)

# Download latest WordPress tar

if run(‘ls -al /%s/latest.tar.gz’ % env.user, warn_only=True).succeeded:

run (‘echo “WordPress already downloaded”‘)


run(‘yum install wget -y && wget’)

#Untar WordPress tar to /var/www/html

if run(‘ls -al /%s/latest.tar.gz’ % env.user, warn_only=True).succeeded:

run (‘tar -xvzf /%s/latest.tar.gz -C %s’ % (env.user, env.dir))


run(‘echo “WordPress file not found or httpd not installed”‘)

#change permission of WordPress directory

if run(‘ls -al %s/wordpress’ % env.dir, warn_only=True).succeeded:

run (‘chown -R apache.apache %s/wordpress && cp -r %s/wordpress/wp-config-sample.php %s/wordpress/wp-config.php’ % (env.dir, env.dir, env.dir))


run(‘echo “WordPress file not found”‘)

#adding DB, username, password, DBhost in wp-config.php file

if run(‘ls -al %s’ % wpconfigfile, warn_only=True).succeeded:

run (‘sed -i \’s/database_name_here/%s/g\’ %s && sed -i \’s/username_here/%s/g\’ %s && sed -i \’s/password_here/%s/g\’ %s && sed -i \’s/localhost/%s/g\’ %s’ % (dbname, wpconfigfile, dbuser, wpconfigfile, dbpassword, wpconfigfile, dbhost, wpconfigfile))


run(‘echo “WordPress file not found”‘)

def plugin_download():

#Download developed plugin code

with cd(‘/home/user/’):

local(‘git clone https://<username>:<password>’)

with cd(‘/home/user/’):

# Zip up deployable files

local(‘zip -r myplugin’)

put(‘/home/user/’, ‘/tmp’)

# Unzip to the target dir

run(‘yum install unzip -y && unzip /tmp/ -d /var/www/html/wordpress/wp-content/plugins’)

# Clean-up

run(‘rm /tmp/’)

def start_httpd():

run(‘mv /etc/httpd/conf.d/welcome.conf /opt/’)

run(‘systemctl start php-fpm’)

run(‘systemctl start httpd’)

def wordpress_deploy():





WordPress installation assumes that user has already setup and installed MySql on another host. Also created user, database grant appropriate permission to user for WordPress application in MySql.

Installation of WordPress application and deployment of plugin are not interdependent to each other. User can execute them independently like only install WordPress (e.g. fab wordpress) or install WordPress with custom plugin deployment ( e.g. fab wordpress_deploy).

Application deployments should also be kept as simple as possible to avoid having too much logic and scope for error built into the deployment scripts.

Install WordPress and deploy plugin



Fabric can be used for multiple purposes, including application deployment, restarting of servers, stopping and restarting processes. Tasks are written in Python after which Fabric executes the code on remote machines. Fabric is simple, powerful and comes with a really good documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Reload Image