Fabric python

What is it?

Fabric is a Python library designed to execute shell commands remotely over ssh. It provides a set of high level commands, and relies on a couple of other libraries, Invoke and Paramiko.

How to install fabric

In Ubuntu, there is a version available in the standard repositories, currently fabric1.14.0-1

sudo apt install fabric

How to use it – the basics

What follows is definitely only the basics. Because fabric is Python based it is very capable and can be used to manipulate python objects. Here we are only looking at simple methods for running commands on remote machines.

In simple terms, having installed fabric, from the command line you can type;

fab <host or host list> <command>

To use a single machine

-H <hostname>

To define a list of machines

def my_machines():
env.hosts = [“compo”, “clegg”, “foggy”, “nora”]

or to open a text file containing a list of machines

def lxworkstations():    

env.hosts = open(‘/usr/home/mnash/fabric/linux_workstations.txt’, ‘r’).readlines()