Fabric – Automate Your Linux Administration Tasks and Application Deployments Over SSH

When it comes to managing remote machines and deployment of applications, there are several command line tools out there in existence though many have a common problem of lack of detailed documentation.

In this guide, we shall cover the steps to introduce and get started on how to use fabric to improve on administering groups of servers.

Automate Linux Administration Tasks Using Fabric

Automate Linux Administration Tasks Using Fabric

Fabric is a python library and a powerful command line tool for performing system administration tasks such as executing SSH commands on multiple machines and application deployment.

Read Also: Use Shell Scripting to Automate Linux System Maintenance Tasks

Having a working knowledge of Python can be helpful when using Fabric, but may certainly not be necessary.

Reasons why you should choose fabric over other alternatives:

  1. Simplicity
  2. It is well-documented
  3. You don’t need to learn another language if you’re already a python guy.
  4. Easy to install and use.
  5. It is fast in its operations.
  6. It supports parallel remote execution.

How to Install Fabric Automation Tool in Linux

An important characteristic about fabric is that the remote machines which you need to administer only need to have the standard OpenSSH server installed. You only need certain requirements installed on the server from which you are administering the remote servers before you can get started.

Requirements:

  1. Python 2.5+ with the development headers
  2. Python-setuptools and pip (optional, but preferred) gcc

Fabric is easily installed using pip (highly recommended), but you may also prefer to choose your default package manager yum, dnf or apt-get to install fabric package, typically called fabric or python-fabric.

For RHEL/CentOS based distributions, you must have EPEL repository installed and enabled on the system to install fabric package.

# yum install fabric   [On RedHat based systems]  
# dnf install fabric   [On Fedora 22+ versions]

For Debian and it’s derivatives such as Ubuntu and Mint users can simply do apt-get to install the fabric package as shown:

# apt-get install fabric

If you want to install development version of fabric, you may use pip to grab the most recent master branch.

# yum install python-pip       [On RedHat based systems] 
# dnf install python-pip       [On Fedora 22+ versions]
# apt-get install python-pip   [On Debian based systems]

Once pip has been installed successfully, you may use pip to grab the latest version of fabric as shown:

# pip install fabric

How to Use Fabric to Automate Linux Administration Tasks

So lets get started on how you can use Fabric. During the installation process, a Python script called fab was added to a directory in your path. The fab script does all the work when using fabric.

Executing commands on the local Linux machine

By convention, you need to start by creating a Python file called fabfile.py using your favorite editor. Remember you can give this file a different name as you wish but you will need to specify the file path as follows:

# fabric --fabfile /path/to/the/file.py

Fabric uses fabfile.py to execute tasks. The fabfile should be in the same directory where you run the Fabric tool.

Example 1: Let’s create a basic Hello World first.

# vi fabfile.py

Add these lines of code in the file.

def hello():
       print('Hello world, Tecmint community')

Save the file and run the command below.

# fab hello
Fabric Tool Usage

Fabric Tool Usage

Let us now look at an example of a fabfile.py to execute the uptime command on the local machine.

Example 2: Open a new fabfile.py file as follows:

# vi fabfile.py

And paste the following lines of code in the file.

#!  /usr/bin/env python
from fabric.api import local
def uptime():
  local('uptime')

Then save the file and run the following command:

# fab uptime
Fabric: Check System Uptime

Fabric: Check System Uptime

Executing commands on remote Linux machines to automate tasks

The Fabric API uses a configuration dictionary which is Python’s equivalent of an associative array known as env, which stores values that control what Fabric does.

The env.hosts is a list of servers on which you want run Fabric tasks. If your network is 192.168.0.0 and wish to manage host 192.168.0.2 and 192.168.0.6 with your fabfile, you could configure the env.hosts as follows:

#!/usr/bin/env python
from  fabric.api import env
env.hosts = [ '192.168.0.2', '192.168.0.6' ]

The above line of code only specify the hosts on which you will run Fabric tasks but do nothing more. Therefore you can define some tasks, Fabric provides a set of functions which you can use to interact with your remote machines.

Although there are many functions, the most commonly used are:

  1. run – which runs a shell command on a remote machine.
  2. local – which runs command on the local machine.
  3. sudo – which runs a shell command on a remote machine, with root privileges.
  4. Get – which downloads one or more files from a remote machine.
  5. Put – which uploads one or more files to a remote machine.

Example 3: To echo a message on multiple machines create a fabfile.py such as the one below.

#!/usr/bin/env python
from fabric.api import env, run
env.hosts = ['192.168.0.2','192.168.0.6']
def echo():
      run("echo -n 'Hello, you are tuned to Tecmint ' ")

To execute the tasks, run the following command:

# fab echo
Fabric: Automate Linux Tasks on Remote Linux

Fabric: Automate Linux Tasks on Remote Linux

Example 4: You can improve the fabfile.py which you created earlier on to execute the uptime command on the local machine, so that it runs the uptime command and also checks disk usage using the df command on multiple machines as follows:

#!/usr/bin/env python
from fabric.api import env, run
env.hosts = ['192.168.0.2','192.168.0.6']
def uptime():
      run('uptime')
def disk_space():
     run('df -h')

Save the file and run the following command:

# fab uptime
# fab disk_space
Fabric: Automate Tasks on Multiple Linux Systems

Fabric: Automate Tasks on Multiple Linux Systems

Automatically Deploy LAMP Stack on Remote Linux Server

Example 4: Let us look at an example to deploy LAMP (Linux, Apache, MySQL/MariaDB and PHP) server on a remote Linux server.

We shall write a function that will allow LAMP to be installed remotely using root privileges.

For RHEL/CentOS and Fedora
#!/usr/bin/env python
from fabric.api import env, run
env.hosts = ['192.168.0.2','192.168.0.6']
def deploy_lamp():
  run ("yum install -y httpd mariadb-server php php-mysql")
For Debian/Ubuntu and Linux Mint
#!/usr/bin/env python
from fabric.api import env, run
env.hosts = ['192.168.0.2','192.168.0.6']
def deploy_lamp():
  sudo("apt-get install -q apache2 mysql-server libapache2-mod-php5 php5-mysql")

Save the file and run the following command:

# fab deploy_lamp

Note: Due to large output, it’s not possible for us to create a screencast (animated gif) for this example.

Now you can able to automate Linux server management tasks using Fabric and its features and examples given above…

Some Useful Options to Use with Fabric

  1. You can run fab –help to view help information and a long list of available command line options.
  2. An important option is –fabfile=PATH that helps you to specify a different python module file to import other then fabfile.py.
  3. To specify a username to use when connecting to remote hosts, use the –user=USER option.
  4. To use password for authentication and/or sudo, use the –password=PASSWORD option.
  5. To print detailed info about command NAME, use –display=NAME option.
  6. To view formats use –list option, choices: short, normal, nested, use the –list-format=FORMAT option.
  7. To print list of possible commands and exit, include the –list option.
  8. You can specify the location of config file to use by using the –config=PATH option.
  9. To display a colored error output, use –colorize-errors.
  10. To view the program’s version number and exit, use the –version option.

Summary

Fabric is a powerful tool and is well documented and provides easy usage for newbies. You can read the full documentation to get more understanding of it. If you have any information to add or incase of any errors you encounter during installation and usage, you can leave a comment and we shall find ways to fix them.

Reference: Fabric documentation

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

7 Responses

  1. cent says:

    Interesting tool, I heard a lot from educational sites before but…

    suppose 2 virtual machines hosting many client sites, say one in New York (NY) and another in Berlin (B).
    Suppose only your NY machine provides visual root login and works as a shepherd. How would you manage your NY or B machine?

    Quite easy: 1) create the php login subsystem in both machnes, 2) sftp bash scripts in both machnes, 3) create the interface between php and bash scripts in NY machine, 4) call commands on shepard that will be forwarded to client server. Done!

    The shell is powerfull, the only danger comes from login hijacking but then you have https, ip checking, ip black/white listing etc.

    Tell you the truth it worths making a bash template script with extended use of the source bash command and there you have it: config files, parameters in files, utility files (is integer, is character), task specific files etc.

    Nothing helpful messing with another language on top of bash I think or…. you can mesh with just because you can but see it as another project on top of the previous giant list!

  2. Indi says:

    That’s a great tool. Thanks for sharing. Found one error though.
    In your last example, the second line should read:
    from fabric.api import env, sudo

  3. Ankammarao says:

    Very useful explanation for newbies and I am a regular follower of your updated thanks

  4. Thorsten says:

    which sreencast do you use for this?

Got something to say? Join the discussion.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.