1 执行一个简单的task任务,显示两台机器的/home/guol/目录下的文件
from fabric.api import *
env.users='root'
env.port=22
env.hosts=['10.1.6.186','10.1.6.159']
#env.hosts=open('/opt/hosts').readlines()
env.password='xxxxxx'
def task1():
with cd('/home/guol'):
run('ls -l')
2.不同的机器执行不同的task
#!/usr/bin/python
from fabric.api import *
from fabric.colors import *
env.roledefs={'web1':['10.1.6.186'],'web2':['10.1.6.159']}
env.password='xxxxxx'
@roles('web1')
def task1():
with cd('/home/guol'):
run('ls -l')
@roles('web2')
def task2():
print(green("I'm fabric"))
def deploy():
execute(task1)
execute(task2)