执行本机操作
  简单的本地操作:
from fabric.api import local
def lsfab():
local('cd ~/tmp/fab')
local('ls')
  结果:
[ken@~/tmp/fab$] pwd;ls
/Users/ken/tmp/fab
fabfile.py   fabfile.pyc  test.py      test.pyc
[ken@~/tmp/fab$] fab -f test.py lsfab
[localhost] local: cd ~/tmp/fab
[localhost] local: ls
fabfile.py  fabfile.pyc test.py     test.pyc
Done.
  实战开始:
  假设,你每天要提交一份配置文件settings.py到版本库(这里没有考虑冲突的情况)
  如果是手工操作:
  cd /home/project/test/conf/
  git add settings.py
  git commit -m 'daily update settings.py'
  git pull origin
  git push origin
  也是说,这几个命令你每天都要手动敲一次,所谓daily job,是每天都要重复的,机械化的工作,让我们看看用fabric怎么实现一键搞定:(其实用shell脚本可以直接搞定,但是fab的优势不是在这里,这里主要位后面本地+远端操作做准备,毕竟两个地方的操作写一种脚本便于维护)
  from fabric.api import local
  def setting_ci():
  local("cd /home/project/test/conf/")
  local("git add settings.py")
  #后面你懂的,懒得敲了…..
  混搭整合远端操作
  这时候,假设,你要到机器A的/home/ken/project对应项目目录把配置文件更新下来
#!/usr/bin/env python
# encoding: utf-8
from fabric.api import local,cd,run
env.hosts=['user@ip:port',] #ssh要用到的参数
env.password = 'pwd'
def setting_ci():
local('echo "add and commit settings in local"')
#刚才的操作换到这里,你懂的
def update_setting_remote():
print "remote update"
with cd('~/temp'):   #cd用于进入某个目录
run('ls -l | wc -l')  #远程操作用run
def update():
setting_ci()
update_setting_remote()
  然后,执行之:
[ken@~/tmp/fab$] fab -f deploy.py update
[user@ip:port] Executing task 'update'
[localhost] local: echo "add and commit settings in local"
add and commit settings in local
remote update
[user@ip:port] run: ls -l | wc -l
[user@ip:port] out: 12
[user@ip:port] out:
Done.
  注意,如果不声明env.password,执行到对应机器时会跳出要求输入密码的交互