How to setup Python fabric on CentOS

Python Fabric is a very good automation tool specially when you have access to a Linux machine via SSH.

This example was tested on CentOS 6.6 docker image:
(Note: You will need epel repository configured on your machine, if its a centos box you can install it by typing: yum install epel-release
So lets start configuring Fabric:

[[email protected] /]# yum search fabric
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.synergyworks.co.uk
* epel: mirror.bytemark.co.uk
* extras: repo.bigstepcloud.com
* updates: centos.hyve.com
fabric.noarch : A simple Pythonic remote deployment tool
[[email protected] /]# yum install fabric

This should install fabric for you.

Now if you haven’t setup SSH key pair on your remote machine that you would like to control via Fabric, you can run the following on your current machine to create key pair, once done copy the public part of the key to authorized_keys file on the remote server:


[[email protected] /]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.

Now for this example I have created another docker instance that is running openssh-server and is configured to accept keys. I have already copied the public key to the other docker instance, so I can log into the second docker instance without providing the password. So lets write the fabfile and run remote commands using Fabric:

[[email protected] ~]# cat fabfile.py
from fabric.api import *

def hostname():
run("hostname")
[[email protected] ~]# fab -H 172.17.0.3 hostname
[172.17.0.3] Executing task 'hostname'
[172.17.0.3] run: hostname
[172.17.0.3] out: edf6ac0f4d24
[172.17.0.3] out:
Done.
Disconnecting from 172.17.0.3... done.

In the above case 172.17.0.3 is my second docker instance. Lets try to run another command remotely … how about getting the contents of hosts file. So all we need to do is to add another function in the fab file and call the function for the desired host:

[[email protected] ~]# cat fabfile.py
from fabric.api import *

def hostname():
run("hostname")

def hostsfile():
run("cat /etc/hosts")
[[email protected] ~]# fab -H 172.17.0.3 hostsfile
[172.17.0.3] Executing task 'hostsfile'
[172.17.0.3] run: cat /etc/hosts
[172.17.0.3] out: 172.17.0.3 edf6ac0f4d24
[172.17.0.3] out: 127.0.0.1 localhost
[172.17.0.3] out: ::1 localhost ip6-localhost ip6-loopback
[172.17.0.3] out: fe00::0 ip6-localnet
[172.17.0.3] out: ff00::0 ip6-mcastprefix
[172.17.0.3] out: ff02::1 ip6-allnodes
[172.17.0.3] out: ff02::2 ip6-allrouters
[172.17.0.3] out:
Done.
Disconnecting from 172.17.0.3... done.

So fabric is lovely little tool to run remote commands on the server. Ofcourse you can use SSH to do all these tasks, but fabric provides you a way of managing scripts (for remote servers) in an efficient and reliable manner.

Leave a Reply

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