Fabric

Fabric is a Python library and a command-line tool that's designed to simplify application deployment and perform system administration tasks through the SSH protocol. It provides tools to execute arbitrary shell commands (either as a normal login user, or via sudo), upload and download files, and so on.

Fabric (http://www.fabfile.org) is a high-level Python (2.7, 3.4+) library that's designed to execute shell commands remotely over SSH so that we can control a group of SSH servers in parallel. It is possible to use Fabric directly from the command line by executing the fab utility or with the API that contains all the classes and decorators that are needed to declare a set of SSH servers, as well as the tasks that we want to execute on them.

One of the main dependencies that must be met before installing Fabric is having the paramiko library installed; this library is responsible for making the connections to the SSH servers using the appropriate authentication mechanism according to each case (auth by password or auth by public key).

Fabric is available in the official Python repository (https://pypi.org/project/Fabric/). We can install Fabric simply by running the following command: 

pip install Fabric

The fundamental element of Fabric from version 2 is the connections. These objects represent the connection to another machine, and we can use it to do the following:

  • Execute commands in the shell of the other machine, which you can run using sudo
  • Download files from the remote machine to local using get
  • Upload files from local to remote using put
  • Do forwarding using forward_local, forward_remote

To start a connection, we need the address of the machine and some way to identify ourselves. In the whole issue of Fabric authentication, it delegates the work in paramiko, which supports a wide variety of options, including the option to use gateways.

Let's look at an example; in this case, we are requesting the IP address and the password for authentication in the remote host:

>>> from getpass import getpass
>>> ip_address= prompt="Enter remote host ip address:")
>> password = getpass(prompt="Enter Password for Connecting with remote host: ")
>>> connection= Connection(host=ip_address,user="user",connect_kwargs={"password" : password})

We can execute commands with the run() and sudo() methods. If we want to obtain the result of the command, we can simply assign a variable for the evaluation of the run commands:

>>> def isLinux(connection):
>>> result = connection.run("uname -s")
>>> return result.stdout.strip() == "Linux"
>>> isLinux(connection)

Fabric is very powerful tool, but as soon as we have many machines, we will often do the same tasks. We can use a simple for loop, but Fabric brings us an abstraction called group. Basically, we can join connections in a single group and execute the actions that we ask. There are two types of groups:

  • SerialGroup: Executes the operations sequentially 
  •  ThreadGroup: Executes the operations in parallel

In this example, we are launching the sudo apt update command in parallel over hosts defined in the ThreadingGroup constructor:

>>> from fabric import ThreadingGroup
>>> def update(cxn):
>>> cxn.run("sudo apt update")
>>> pool = ThreadingGroup("user1@host1","user2@host2")
>>> update(pool)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset