本文共 4380 字,大约阅读时间需要 14 分钟。
[ken@~/tmp/fab$] mv fabfile.py .py fabfile.py -> test.py [ken@~/tmp/fab$] fab hello Fatal error: Couldn't find any fabfiles! Remember that -f can be used to specify fabfile path, and use -h for help. Aborting. [ken@~/tmp/fab$] fab -f test.py hello Hello world! Done. |
[ken@~/tmp/fab$] fab hello:name=age,value=20 age = 20! Done. [ken@~/tmp/fab$] fab hello:age,20 age = 20! Done. |
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. |
#!/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. |
#!/usr/bin/env python # encoding: utf-8 from fabric.api import * #操作一致的服务器可以放在一组,同一组的执行同一套操作 env.roledefs = { 'testserver': ['user1@host1:port1',], 'realserver': ['user2@host2:port2', ] } #env.password = '这里不要用这种配置了,不可能要求密码都一致的,明文编写也不合适。打通所有ssh就行了' @roles('testserver') def task1(): run('ls -l | wc -l') @roles('realserver') def task2(): run('ls ~/temp/ | wc -l') def dotask(): execute(task1) execute(task2) |
[ken@~/tmp/fab$] fab -f mult.py dotask [user1@host1:port1] Executing task 'task1' [user1@host1:port1] run: ls -l | wc -l [user1@host1:port1] out: 9 [user1@host1:port1] out: [user2@host2:port2] Executing task 'task2' [user2@host2:port2] run: ls ~/temp/ | wc -l [user2@host2:port2] out: 11 [user2@host2:port2] out: Done. |
env.hosts = [ 'host1', 'host2' ] env.passwords = { 'host1': "pwdofhost1", 'host2': "pwdofhost2", } 或者 env.roledefs = { 'testserver': ['host1', 'host2'], 'realserver': ['host3', ] } env.passwords = { 'host1': "pwdofhost1", 'host2': "pwdofhost2", 'host3': "pwdofhost3", } |
转载地址:http://bbgma.baihongyu.com/