Cloud Computing

5 Mins Read

Fabric – Dive into Usage

In my previous blog article on Fabric i.e. Fabric – Automate Administrative Tasks with Ease we learnt:

In this article, we will cover,

Procedures Provided by Fabric

Following are the most beneficial procedures (commands or tools) that comes with Fabric to make the operations even more easier. We will use all these procedures inside the fabfile after installing Fabric.

While dealing with the remote hosts, run procedure is used to execute the shell commands. Using some variables, the output can also be stored for either future use or as log. Any shell command that we want to execute on one or more remote host needs to be placed inside the bracket.
run(“mkdir /home/fabric”)

In most cases, we need to run the commands as a superuser. Using sudo is the most commonly used procedure other than run. The same way we used run, we will use sudo by placing the desired shell command inside the brackets.
sudo(“mkdir /home/fabric”)

As stated above, fabric allows us to do the automation locally just like the way it allows us to automate on remote machines. For that purpose, it provides the local utility to execute commands locally. Only limitation in local procedure is, we cannot store the output. However, run and sudo procedure lets you store the output.
local(“python /home/python/”)

For downloading files from the remote hosts, get() is used. We need to specify the path of source on remote and destination on local. For uploading files to the remote hosts, put() is used. Just like in get(), here also we need to specify source on local and destination on remote.

For downloading a database backup
get("/backup/prod1db.gz", "./db.gz")

For uploading a tar to remote
put("/local/path/to/app.tar.gz", "/tmp/trunk/app.tar.gz")

One of my favorite procedure, prompt() lets you take user input just by making users to give the input on the prompt.
prompt('Enter the preferred username for this script:', 'pyuser')


For the demo purpose, I will be working on EC2 using RHEL 7 AMI

Fabric can be installed with very easy and quick installation process with minimal system requirements. Below are the system requirements

If you are using the RHEL 7 AMI, then it will have first two requirements already installed on it. You will just need to download and install EPEL Repository.

  1. Download and install EPEL Repo
    $ yum install wget
    $ wget
    $ yum install epel-release-7-10.noarch.rpm
  2. Update the system and install Fabric
    yum update
    yum install fabric

Executing Fabric Locally

Once Fabric is installed, we move towards the main pillar of Fabric i.e. fabfile – the python scripts. We will see how to create and code as per our requirements as well as how to execute them

    1. Create the fabfile
    2. Put the below content in the file
      def welcome():
      print(“Welcome to CloudThat to learn and moveUP”)
    3. Now save and exit the file
    4. As discussed in previous article, Fabric comes with inbuilt fab tools
    5. Using this fab tool, we will execute the function that we wrote in the fabfile locally as below
      fab welcome
    6. Currently, we have just one function in the file that we executed above, but we can do many other operations using Fabric easily

Executing Fabric on Remote Servers

Now, we will execute some commands on remote servers to see how Fabric’s actual working is


    • For executing any command or to configure anything on remote servers, the servers must be able to communicate over SSH with each other
    • If such setup is already ready with you, then you can use that one and if not, you will need to generate a new key and share it with remote servers
    • Make sure the servers do not need any password to access it.
  1. In this demo, I will use two remote servers with which the Fabric machine will communicate
  2. Once you are ready with such setup, open the fabfile and add the below content in the file
    • To import all Fabric APIs without which we will not be able to execute the commands
    • To specify the remote servers and
    • Some other functions that we want to run on those servers

    #!/usr/bin/env python
    # Import Fabric's API module
    from fabric.api import *

    #Set the hosts on which you want to execute the file
    env.hosts = [

    def welcome():
    print("Welcome to CloudThat to learn and moveUP")

    def uptime():

    def diskspace_available():
    run('df -h')

    def create_file():
    run('touch demo.txt')

  3. Now, similar to executing the function locally, we will execute it on remote servers
    fab uptime

    fab diskspace_available
  4. Above we looked at commands for gathering few basic information
  5. Now, we will set user and install some package on the remote servers. For that, add the below content in the same file
    #Set the username
    env.user = "root"

    #Update remote machines
    def update():
    run("yum update -y")

    #Install memcached
    def install_memcached():
    run("yum install memcached -y")

    #Execute all functions together
    def exec_all():
  6. Now save and exit the file
  7. Execute the whole script just by calling one function as below
    fab exec_all

If you notice the output, you can observe that Fabric is not idempotent as like other configuration management tools, but it is very useful and fast for routine administrative tasks.

Please feel free to share your views in the comments section below.

If you are new to automation, then you would like to check out our course on Fundamentals for DevOps.




    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!