#!/usr/local/python27/bin/python
#encoding:utf8
from fabric.api import *
host_1 = '192.168.222.139'
host_2 = '192.168.222.140'
env.hosts = [host_1,host_2]
env.user = 'root'
def taskA():
run('ls')
def taskB():
run('whoami')
可以通过
env.hosts来完成主机的定义
env.user完成用户的定义
[root@web2 manage_host]# /usr/local/python27/bin/fab -f 1.py taskA
[192.168.222.139] Executing task 'taskA'
[192.168.222.139] run: ls /opt
[192.168.222.139] out: index.html
[192.168.222.139] out:
[192.168.222.140] Executing task 'taskA'
[192.168.222.140] run: ls /opt
[192.168.222.140] out: index.html
[192.168.222.140] out:
Done.
Disconnecting from 192.168.222.139... done.
Disconnecting from 192.168.222.140... done.
[root@web2 manage_host]# /usr/local/python27/bin/fab -f 1.py taskB
[192.168.222.139] Executing task 'taskB'
[192.168.222.139] run: whoami
[192.168.222.139] out: root
[192.168.222.139] out:
[192.168.222.140] Executing task 'taskB'
[192.168.222.140] run: whoami
[192.168.222.140] out: root
[192.168.222.140] out:
Done.
Disconnecting from 192.168.222.139... done.
Disconnecting from 192.168.222.140... done.
[root@web2 manage_host]# /usr/local/python27/bin/fab -f 1.py taskA taskB
[192.168.222.139] Executing task 'taskA'
[192.168.222.139] run: ls /opt
[192.168.222.139] out: index.html
[192.168.222.139] out:
[192.168.222.140] Executing task 'taskA'
[192.168.222.140] run: ls /opt
[192.168.222.140] out: index.html
[192.168.222.140] out:
[192.168.222.139] Executing task 'taskB'
[192.168.222.139] run: whoami
[192.168.222.139] out: root
[192.168.222.139] out:
[192.168.222.140] Executing task 'taskB'
[192.168.222.140] run: whoami
[192.168.222.140] out: root
[192.168.222.140] out:
Done.
Disconnecting from 192.168.222.139... done.
Disconnecting from 192.168.222.140... done
用于我这里做面密码验证所以没有提示输入密码
另外我们还可以单独指定主机
#!/usr/local/python27/bin/python
#encoding:utf8
from fabric.api import *
host_1 = '192.168.222.139'
host_2 = '192.168.222.140'
env.hosts = [host_1,host_2]
env.user = 'root'
@hosts(host_1)
def taskA():
run('ls /opt')
@hosts(host_2)
def taskB():
run('whoami')
这样就把全局的主机设置改变了,以装饰起的方式来指定任务在那个主机上执行,不指定的话默认在定义的所有主机上执行,
上面就相当于taskA只在host_1上执行,taskB只在host_2上执行
以上的定义对主机较少还比较使用,如果服务器数量多了,而且各种服务器具有一定角色的时候,上面的定义就显得麻烦了
#!/usr/local/python27/bin/python
#encoding:utf8
from fabric.api import *
www_1 = '192.168.222.139'
www_2 = '192.168.222.140'
www_3 = '192.168.222.141'
db_1 = '192.168.222.142'
db_2 = '192.168.222.143'
env.roledefs = {
'web':[www_1,www_2,www_3],
'db':[db_1,db_2]
}
@roles('db')
def initdb():
print 'initdb'
@roles('web')
def copy_files():
run('ls /opt')
@roles('db')
def migrate():
run('whoami')
这样采用角色分组来定义主机将会更加简单和自由。
下面是更加复杂的一种情况
#!/usr/local/python27/bin/python
#encoding:utf8
from fabric.api import *
www_1 = '192.168.222.139'
www_2 = '192.168.222.140'
www_3 = '192.168.222.141'
db_1 = '192.168.222.142'
db_2 = '192.168.222.143'
env.roledefs = {
'web':[www_1,www_2,www_3],
'db':[db_1,db_2]
}
@roles('db')
def initdb():
print 'initdb'
@roles('web')
def copy_files():
run('ls /opt')
@roles('db')
def migrate():
run('whoami')
def deploy():
#initdb()
#copy_files()
#migrate()
execute(initdb,hosts=[db_1])
execute(copy_files)
execute(migrate)
需要对任务进行顺序依赖的时候
如果直接调用会出现以下情况
No hosts found. Please specify (single) host string for connection:
这时就要使用到execute来执行,并且还可以在其中指定主机
[192.168.222.142] Executing task 'initdb'
initdb
[192.168.222.139] Executing task 'copy_files'
[192.168.222.139] run: ls /opt
[192.168.222.139] out: index.html
[192.168.222.139] out:
[192.168.222.140] Executing task 'copy_files'
[192.168.222.140] run: ls /opt
[192.168.222.140] out: index.html
[192.168.222.140] out:
[192.168.222.141] Executing task 'copy_files'
[192.168.222.141] run: ls /opt
[192.168.222.141] out: index.html
[192.168.222.141] out:
[192.168.222.142] Executing task 'migrate'
[192.168.222.142] run: whoami
[192.168.222.142] out: root
[192.168.222.142] out:
[192.168.222.143] Executing task 'migrate'
[192.168.222.143] run: whoami
[192.168.222.143] out: root
[192.168.222.143] out:
Done.
Disconnecting from 192.168.222.141... done.
Disconnecting from 192.168.222.143... done.
Disconnecting from 192.168.222.139... done.
Disconnecting from 192.168.222.142... done.
Disconnecting from 192.168.222.140... done.
可以看initdb只在db_1执行,copy_files在所有的web角色上执行了,而migrate只在db角色上执行了,这样fabric就可以灵活进行主机设置和定义