Python fabric远程自动部署简介

发表于:2014-7-15 11:27

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:安生    来源:51Testing软件测试网采编

分享:
  3. 文件名不为fabfile.py时需进行指定
  # mv fabfile.py test.py
  # fab hello
  Fatal error: Couldn't find any fabfiles!
  Remember that -f can be used to specify fabfile path, and use -h for help.
  # fab -f test.py hello
  Hello fab!
  4. 参数传递
  #vi fabfile.py
  def hello(name):
  print 'Hello %s!'%name
  # fab hello:name=fab
  Hello fab!
  # fab hello:fab
  Hello fab!
  2.2.    本地操作
  执行本地操作命令使用local
  1. fabfile.py脚本内容如下
  from fabric.api import local
  def test():
  local('cd /home/')
  local('ls -l|wc -l')
  2. 执行命令fab test,结果如下
  # fab test
  [localhost] local: cd /home/
  [localhost] local: ls -l|wc -l
  2.3.    远程操作
  执行远程操作命令使用run
  1. fabfile.py脚本内容如下
  from fabric.api import cd,run,env,hosts
  env.hosts=['192.168.85.99:22','192.168.85.101:22']
  env.password='test'
  def test():
  with cd('/home'):
  run("du -sh")
  2. 执行命令fab test,结果如下
  # fab test
  [192.168.85.99:22] Executing task 'test'
  [192.168.85.99:22] run: du -sh
  [192.168.85.99:22] out: 392G      .
  [192.168.85.99:22] out:
  [192.168.85.101:22] Executing task 'test'
  [192.168.85.101:22] run: du -sh
  [192.168.85.101:22] out: 5.6G     .
  [192.168.85.101:22] out:
  Disconnecting from 192.168.85.99... done.
  Disconnecting from 192.168.85.101... done.
  3. 多服务器混合,需要在不同服务器进行不同操作时,可参考如下脚本
  from fabric.api import env,roles,run,execute
  env.roledefs = {
  'server1': ['root@192.168.85.99:22',],
  'server2': ['root@192.168.85.100:22', ]
  }
  env.password = 'test'
  @roles('server1')
  def task1():
  run('ls /home/ -l | wc -l')
  @roles('server2')
  def task2():
  run('du -sh /home')
  def test():
  execute(task1)
  execute(task2)
  结果如下
  # fab test
  [root@192.168.85.99:22] Executing task 'task1'
  [root@192.168.85.99:22] run: ls /home/ -l | wc -l
  [root@192.168.85.99:22] out: 27
  [root@192.168.85.99:22] out:
  [root@192.168.85.100:22] Executing task 'task2'
  [root@192.168.85.100:22] run: du -sh /home
  [root@192.168.85.100:22] out: 1.4G   /home
  [root@192.168.85.100:22] out:
  Disconnecting from 192.168.85.99... done.
  Disconnecting from 192.168.85.100... done.
22/2<12
重磅发布,2022软件测试行业现状调查报告~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2023
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号