Python

Python fabric

Linux users must repeatedly perform different types of administrative or general tasks, such as reloading the apache server after making any change, creating a new application or deploying any application, accessing the particular log files, etc. SSH(Secure Shell) is required to do these types of tasks regularly. Fabric is a powerful and helpful Python library that interacts with the SSH and operating system to automate many application development or administrative tasks. This command-line tool is very simple and easy to use. It works faster, supporting parallel remote execution. How fabric library can be installed and used in python3+ has shown in this tutorial.

Useful Options of fabric:

It has many useful options to perform different types of tasks. Some mostly used options of this tool have described below.

Fabric Option Description
–fabfile=PATH It is used to define the Python module file.
–user=USER It is used to define the username to connect with the host.
–password=PASSWORD It is used to define the password to connect with the host.
–display=NAME It is used to display detailed information about the fab command.
–list-format=FORMAT It is used to set the specific format option.
–config=PATH It is used to define the location of the config file.
–colorize-errors It is used to display the error with the color.
–version It is used to display the version of the fab command.
–help It is used to display detailed information about the available options of the fab command.

Install fabric in Python3:

Run the following command to install the fabric command in Python3.

$ pip3 install fabric3

Example-1: Use of fabric command without ssh

A python file named fabfile.py is required by the fab command to do some specific tasks automatically. Create the fabfile.py file with the following script. message() function has defined in the script that will take the name from the user and print the welcome message. This function name is required to use with the fab command to execute the script.

fabfile.py

# Define function
def message():
   # Take a name from the user
   name = input('Enter your name : ')
   # Print the name with greeting
   print('Hello, %s' %name)

Run the following command to execute the script of the fabfile.py.

$ fab message

The following output will appear after giving the value of the name.

Installing SSH on Ubuntu:

SSH is used to interact with the remote server, and the fabric command can be executed by connecting with the remote server through SSH. It is not installed on Ubuntu by default. openssh-server will require to install for performing SSH activities from the local computer. After installing this package, the fab command can perform some tasks remotely by using an SSH connection.

Run the following commands from the terminal to update the system and install the openssh-server.

$ sudo apt update

$ sudo apt install openssh-server

Run the following command to check the ssh command is working properly or not.

$ ssh localhost

The following output will appear after typing ‘yes‘ if the ssh command is working properly.

Example-2: Start, Stop and check the status of Apache Server using fabric

Create or modify the fabfile.py with the following script. The run module has been imported in the script to run the commands to start, stop, and check the apache server’s status. env module has been imported to set the hosts value. start_apache() function has been defined to start the apache server. stop_apache() function has been defined to stop the apache server. status_apache() function has been defined to check the current status of the apache server. A valid username and password have to provide for ssh connection when executing the fab command.

fabfile.py

# Import run and env from fabric.api
from fabric.api import run, env

# Set the hosts name
env.hosts = '127.0.0.1'

# Define function to start Apache server
def start_apache():
    run('sudo systemctl start apache2')
    print('Apache server is started.')

# Define function to stop Apache server
def stop_apache():
    run('sudo systemctl stop apache2')
    print('Apache server is stopped.')

# Define function to check the status of Apache server
def status_apache():
    run('sudo systemctl status apache2')

Run the following command from the terminal to start the apache server with the fab command.

$ fab --user-fahmida --password=12345 start_apache

Run the following command from the terminal to check the apache server’s status with the fab command.

$ fab --user-fahmida --password=12345 status_apache

Run the following command from the terminal to stop the apache server with the fab command.

$ fab --user-fahmida --password=12345 stop_apache

Example-3: Perform multiple tasks using fabric

Create or modify the fabfile.py with the following script. The run module has been imported into the script to get the processor type and the disk’s used space. env module has been imported to set the hosts, user, and password values for the SSH connection. multi_task() function has defined to do the two tasks together.

fabfile.py

# Import run and env modules from Fabric's API
from fabric.api import run, env

# Set the host IP
env.hosts = '127.0.0.1'
# Set the username
env.user = "fahmida"
# Set the username
env.password = "12345"

# Define function to run multiple tasks
def multi_tasks():
    # Print the processor type
    run('uname -p')

    # Print the used space of the disk in a human-readable format
    run('sudo df -h')

Run the following command from the terminal to print the processor type and the detailed information about the disk space.

$ fab multi_tasks

If the given username and password in the script are valid, then the following output will be appeared after executing the above command.

Conclusion:

The regular tasks can be done automatically by using the fabric module of Python. The Linux users can easily perform many administrative-related tasks by executing a simple fab command after writing the script to execute the required commands in the fabfile.py file. The fabric module uses have been explained in this tutorial by using three simple examples to help the readers know the fabric module’s function.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.