def hello():
print("Hello fab!")
执行操作hello:
fab hello
def hello(name):
print "Hello %s" % (name)
from fabric.api import local
def test():
local('cd /home/dev')
local('ls')
from fabric.api import cd, run, env, hosts
env.hosts = ['192.168.101.131:22', '192.168.101.129:22']
env.user = 'root'
env.password = 'toor'
def install_pip():
run('mkdir -p /y01')
cd('/y01')
run('wget https://bootstrap.pypa.io/get-pip.py')
run('python get-pip.py')
以上为多台服务器执行相同操作,当多台服务器执行不同操作时:
from fabric.api import env, roles, run, execute
env.roledefs = {
'server1' : ['[email protected]:22'],
'server2' : ['[email protected]:22']
}
env.user = 'root'
env.password = 'toor'
@roles('server1')
def task1():
run('ls /home')
@roles('server2')
def task2():
run('du -sh /home')
def test():
execute(task1)
execute(task2)