1. 创建一系列任务,通过fab任务执行要执行的任务;
2. 根据主机列表定义,去执行每一个任务;
3. 没有主机定义的任务,将在本地执行一次。
如:
from fabric.api import run, env env.hosts = ['host1', 'host2'] def taskA(): run('ls') def taskB(): run('whoami') # 将在host1, host2都执行taskA, taskB $ fab taskA taskB # 执行顺序如下 taskA executed on host1 taskA executed on host2 taskB executed on host1 taskB executed on host2
username@hostname:port #用户名和端口都可以省略
::1, [::1]:1222 user@2001:db8::1 user@[2001:db8::1]:1222
from fabric.api import env #假如www1, www2, www3是你的web服务器, 你可以指定任务由特定的角色来执行 env.roledefs['webservers'] = ['www1', 'www2', 'www3'] # 定义多个角色 env.roledefs = { 'web': ['www1', 'www2', 'www3'], 'dns': ['ns1', 'ns2'] }
from fabric.api import env, run env.hosts = ['host1', 'host2'] def mytask(): run('ls /var/www')
from fabric.api import env, run def set_hosts(): env.hosts = ['host1', 'host2'] def mytask(): run('ls /var/www') # 调用 fab set_hosts mytask, set_hosts
$ fab -H host1,host2 mytask #会被env.hosts所重写,类似角色也可以通过-R role1, role2, ...来定义
from fabric.api import env, run env.hosts.extend(['host3', 'host4']) def mytask(): run('ls /var/www') # 那么执行主机将merge为host1,host2,host3,host4 fab -H host1, host2 mytask
1
|
|
from fabric.api import run def mytask(): run('ls /var/www') # 该定义将重写其他任何hosts定义,mytask仅会在host1,host2上执行 fab mytask:hosts="host1;host2"
from fabric.api import hosts, run @hosts('host1', 'host2') def mytask(): run('ls /var/www') # 或者 my_hosts = ('host1', 'host2') @hosts(my_hosts) def mytask(): # ... # 这种修饰将重写env.hosts设置, 但不会重写上面通过命令行定义的任务。
# 由高到低 1. 命令行 fab mytask:host=host1 2. fabfile中修饰任务@hosts('host1') 3. 在fabfile中设置env.hosts = ['host1'] 4. 在命令行中-H host1,host2,...
from fabric.api import env, hosts, roles, run env.roledefs = {'role1': ['b', 'c']} @hosts('a', 'b') @roles('role1') def mytask(): run('ls /var/www') # 若命令行不包含task定义,则上面的mytask家在a, b, c主机上执行
# 若myrole被定义运行[host1, host2,...,host15], 则该命令将排除host2,host5 $ fab -R myrole -x host2,host5 mytask #该-x并不会修改env.hosts
from fabric.api import run, roles env.roledefs = { 'db': ['db1', 'db2'], 'web': ['web1', 'web2', 'web3'], } @roles('db') def migrate(): # Database stuff here. pass @roles('web') def update(): # Code updates here. pass # 在fabric1.2之前,我们需要fab migrate update来执行这两个任务, # 在fabric1.2之后,我们可以通过execute函数来执行这两个任务: from fabric.api import run, roles, execute def deploy(): execute(migrate) execute(update)
from fabric.api import env, roles, task, execute, run, runs_once env.roledefs = { 'zoo': ['zooserver'], 'mysql': ['mysqlmaster'], } @task def workhorse(): return run("uname -a") @roles('zoo') @task @runs_once def go(): results = execute(workhorse) print results #执行 fab go
from fabric.api import run, execute, task # For example, code talking to an HTTP API, or a database, or ... from mylib import external_datastore # This is the actual algorithm involved. It does not care about host # lists at all. def do_work(): run("something interesting on a host") # This is the user-facing task invoked on the command line. @task def deploy(lookup_param): # This is the magic you don't get with @hosts or @roles. # Even lazy-loading roles require you to declare available roles # beforehand. Here, the sky is the limit. host_list = external_datastore.query(lookup_param) # Put this dynamically generated host list together with the work to be # done. execute(do_work, hosts=host_list) # 调用 fab deploy:app fab deploy:other
from fabric.api import run, task from mylib import external_datastore # Marked as a publicly visible task, but otherwise unchanged: still just # "do the work, let somebody else worry about what hosts to run on". @task def do_work(): run("something interesting on a host") @task def set_hosts(lookup_param): # Update env.hosts instead of calling execute() env.hosts = external_datastore.query(lookup_param) #调用 fab set_hosts:app do_work #相比上一种方法,该方法可以轻易替换其他任务,如 fab set_hosts:db snapshot fab set_hosts:cassandra,cluster2 repair_ring fab set_hosts:redis,environ=prod status
from fabric.api import * @hosts('host1') def clean_and_upload(): local('find assets/ -name "*.DS_Store" -exec rm '{}' \;') local('tar czf /tmp/assets.tgz assets/') put('/tmp/assets.tgz', '/tmp/assets.tgz') //这里才会开始进行远程连接相关的操作:cache.get() == null?new : cache.get() with cd('/var/www/myapp/'): run('tar xzf /tmp/assets.tgz')
User/Port ,HostName, IdentityFile, ForwardAgent, ProxyCommand。
具体参考可见:
http://docs.fabfile.org/en/1.9/usage/execution.html