Automation of Systems Administration Tasks
Jan 31
2010
2010
No need for complex, unclear, and clumsy programs like Capistrano.
Use Fabric, here is a short exmple, which automates the archive process of users’ directories:
# install the Python library easy_install fabric # create a file called "fabfile" # it will be used from the "fab" command # add the following tasks in "fabfile":
from fabric.api import * env.hosts = ["main"] env.user = "root" def archive_home(): with cd("/tmp"): run("zip -rq home_`date +%Y:%m:%d-%H:%M`.zip /home") def archive_download(): import os os.system("scp %s:/tmp/home_*.zip /home/su/archive" % env.hosts[0]) def archive_remove(): run("rm /tmp/home_*.zip")
In addition you can use decorators to specify different hosts for different tasks.
More about the “fab” command:
fab --help
Usage: fab [options] (command) [:arg1,arg2=val2,host=foo,hosts='h1;h2',...] ...
Options:
-h, --help show this help message and exit
-V, --version show program's version number and exit
-l, --list print list of possible commands and exit
-d COMMAND, --display=COMMAND
print detailed info about a given command and exit
-r, --reject-unknown-hosts
reject unknown hosts
-D, --disable-known-hosts
do not load user known_hosts file
-u USER, --user=USER username to use when connecting to remote hosts
-p PASSWORD, --password=PASSWORD
password for use with authentication and/or sudo
-H HOSTS, --hosts=HOSTS
comma-separated list of hosts to operate on
-R ROLES, --roles=ROLES
comma-separated list of roles to operate on
-i KEY_FILENAME path to SSH private key file. May be repeated.
-f FABFILE, --fabfile=FABFILE
name of fabfile to load, e.g. 'fabfile.py' or
'../other.py'
-w, --warn-only warn, instead of abort, when commands fail
-s SHELL, --shell=SHELL
specify a new shell, defaults to '/bin/bash -l -c'
-c RCFILE, --config=RCFILE
specify location of config file to use
--hide=LEVELS comma-separated list of output levels to hide
--show=LEVELS comma-separated list of output levels to show
Comment