3.fabric定义主机

#!/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就可以灵活进行主机设置和定义





 

转载于:https://my.oschina.net/eddylinux/blog/1541036

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值