前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 远程执行代码 fabr

python 远程执行代码 fabr

作者头像
py3study
发布2020-01-14 15:09:06
5690
发布2020-01-14 15:09:06
举报
文章被收录于专栏:python3

 fabric应用:                1、fab -H 172.23.177.40,172.23.177.41 host_type                2、fab host_type check_ver                3、role: 当client较多时可以定义角色,然后按角色进行操作。                     #!/usr/bin/pyhton                     from fabric.api import *                     env.user = 'root'                     env.password = 'vagrant'                     env.roledefs = {                          'web': ['172.23.177.41','172.23.177.43'],                          'dns': ['172.23.177.46','177.23.177.73']                     }                     def test():                         run('uname -a')                run:                     fab -R web test                result:                     [172.23.177.41] Executing task 'test'                     [172.23.177.41] run: uname -a                     [172.23.177.41] out: Linux salt-master 2.6.32-358.23.2.el6.x86_64 #1 SMP Wed Oct 16 18:37:12 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux                     [172.23.177.41] out:                     [172.23.177.43] Executing task 'test'                     [172.23.177.43] run: uname -a                     [172.23.177.43] out: Linux salt-minion-3 2.6.32-358.23.2.el6.x86_64 #1 SMP Wed Oct 16 18:37:12 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux                     [172.23.177.43] out:                     Done.                     Disconnecting from 172.23.177.43... done.                     Disconnecting from 172.23.177.41... done.                     #从结果可以看到,fab只执行了属于web组的client。                4、extend属性                     from fabric.api import env, run                     env.hosts.extend(['host3', 'host4'])                     def test():                         run('uname -r')                     When this fabfile is run as fab -H host1,host2 mytask, env.hosts will then contain ['host1', 'host2', 'host3', 'host4'] at the time that mytask is executed.                5、fabfile中没有定义client,可以在命令行中指定                     fab mytask:hosts='172.23.177.41;172.23.177.46'                6、使用task方式:                     from fabric.api import hosts, run                     @hosts('172.23.177.41', '172.23.188.46')                     def test():                         run('uname -r')                     或者:                     my_hosts = ('172.23.177.41', '172.23.177.46')                     @hosts(my_hosts)                     def test():                          run('uname -r')                run:                     fab test                resule: 从结果中可以看到,fab只执行了task定义的test部分                     [172.23.177.41] Executing task 'test'                     [172.23.177.41] run: uname -a                     [172.23.177.41] out: Linux salt-master 2.6.32-358.23.2.el6.x86_64 #1 SMP Wed Oct 16 18:37:12 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux                     [172.23.177.41] out:                     [172.23.177.46] Executing task 'test'                     [172.23.177.46] run: uname -a                     [172.23.177.46] out: Linux salt-minion-1 2.6.32-358.23.2.el6.x86_64 #1 SMP Wed Oct 16 18:37:12 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux                     [172.23.177.46] out:                     Done.                     Disconnecting from 172.23.177.41... done.                     Disconnecting from 172.23.177.46... done.                7、综合应用:                     from fabric.api import env, hosts, roles, run                     env.roledefs = {'role1': ['b', 'c']}                     @hosts('a', 'b')                     @roles('role1')                     def test():                         run('uname -r')                8、遇到主机不可达的情况,可以使用--skip-bad-hosts参数,这样就会跳过不存在或有问题的client,而执行其他的client,从返回结果中可以排除那些是有问题的client,进而在进行处理                     fab --skip-bad-hosts test                     node:                     my_hosts = ('172.23.177.41','172.23.177.40','172.23.177.46')                     @hosts(my_hosts)                     def test():                         run('uname -a')

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/07/19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档