Tuesday, May 7, 2013

python-fabric. Easing multiple servers administration

I'm deploying a test scenario involving 16 pristine virtual machines and as a lazy administrator I'd hate to do the same configuration 16 times, that's what python-fabric is useful for.

From the official website:

What is Fabric?

As the README says:
Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
More specifically, Fabric is:
  • A tool that lets you execute arbitrary Python functions via the command line;
  • A library of subroutines (built on top of a lower-level library) to make executing shell commands over SSH easy and Pythonic.
Naturally, most users combine these two things, using Fabric to write and execute Python functions, or tasks, to automate interactions with remote servers. Let’s take a look.

This is my first script which copies my public key into the test machines:

from fabric.api import local,env

hosts = []
for i in range(1,17):
   hosts.append( "box%d" % i)

env.hosts= hosts
def test_ssh():
         local("""ssh -o PasswordAuthentication=no root@%s "ifconfig | grep  -E  'inet.*192.168.0' | sed 's/\s\s*/ /g' | cut -d' ' -f3" """%env.host)
      except SystemExit as e:
         local("ssh-copy-id root@%s"%env.host)

If you have to administer multiple machines on a daily basis, I advice you give python-fabric a try.

No comments:

Post a Comment