Fabric Automation

By SupremeOverLord No comments 1k views

Automation is better than manual work. The less we intervene in a process, the better. Everything. Let's also figure out a way to do this using a single tool. One that is easy to program, easy to use. And why not do all this with nothing but SSH installed on the remote machine – all commands scripted at a single location for executing locally or on any number of various servers.

In this blog, we will talk about this amazing tool called Fabric. I will show you how to install it, create simple deployment and management functions that you can run across multiple servers. Yes, you can throw away your bash hacks and complex scripts.

Install Fabric!

I will assume you are using a Unix/Linux System. First of all, install pip

curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"

Then, install fabric

python get-pip.py && pip install Fabric

Fabric brings some amazing ssh automation and integration with python where you can write python functions and code and these will run across multiple servers. Fabric has a huge selection of functions to help with a lot of remote automation tasks. To get started, create a fab file called fabfile.py in any directory you want. We will take a look at few of Fabricfunctions and operations.

The first operation is run (fabric.operations.run). The run feature is used for executing a shell command on one or more remote hosts. You can capture the output of this feature in a variable and pass it in your script. Here is a simple example:

# Check Memory
run('free -m')

# Check hostname

# Check Disk Space
run('df -h')

Fabric sudo feature

This command allows you to execute code as a super-user with root privileges on the remote system

# Create a directory
sudo('mkdir /usr/local/firewall')

# Install Apache WebServer
sudo('yum -y install httpd')

# Yum Upgrade
sudo("yum -y upgrade",pty=True)

Fabric get feature

The get is used to download (i.e. pull) file(s) from the remote system to the computer where the Fabric is being used The remote path is identified by the remote_path argument. The local download path is specified by the local_path argument

# Download Apache logs & store them locally
get(remote_path="/var/log/httpd/logs.tar.gz", local_path="logs.tar.gz")

Fabric put feature

The get is used to put (i.e. upload) file(s) from the local system to the remote server(s) The remote path is identified by the remote_path argument. The local download path is specified by the local_path argument

The mode argument set the file mode.
# Upload a tar archive of a local file to the servers
put(local_path=/var/www/local.tar.gz", remote_path="/tmp/trunk/app.tar.gz")

# Upload a file and set mode
upload = put("requirements.txt", "requirements.txt", mode=664)

# Verify the upload

Fabric reboot feature

The reboot command straight-forward: it is used to reboot the remote system. By default, it waits two minutes (i.e. 120 seconds -> wait=120) before doing its job.

# Reboot the remote system

# # Reboot after 20 seconds

An example fabfile.py

Let us create an empty fabfile.py to show you how this works.

vi fabfile.py
# import all the fabric functions that we
from fabric.api import *
# import the os module to get file basenames
import os
# Define Hosts you want to run commands on
env.hosts = ['','']
env.user = "username"
env.password = "passwordcomeshere" #Use ssh keys for authentication instead
env.parallel = False

def uptime():
  """Check the uptime on Servers"""
  with hide('running','warnings'):

def disk_space():
  """Check the Diskspace on Servers"""
  with hide('running','warnings'):
    run('df -h')

def memory():
  """Check the Diskspace on Servers"""
  with hide('running','warnings'):
    run('free -m')

def os-type():
  """Check OS Type"""
  with hide('running','warnings'):
    run('cat /etc/issue')

Run fabric: In this case, check the memory on the servers fab memory

fab memory
[] Executing task 'memory'
[] Executing task 'memory'
[] out:
[] out:
[] out:               total        used        free      shared  buff/cache   available
[] out: Mem:           1504          97        1368           5          37        1342
[] out: Swap:          1504         364        1139
[] out:
[] out: supremeoverlord.io
[] out:
[] out:              total       used       free     shared    buffers     cached
[] out: Mem:           996        264        731          0         37        163
[] out: -/+ buffers/cache:         64        932
[] out: Swap:            0          0          0
[] out:


That's fab for you. I will update this blog in the coming weeks on how I use fab for automated deployment to multiple tomcat servers. Enjoy!