python 远程执行代码 fabr

发布时间:2019-07-19 09:53:57编辑:auto阅读(1448)

     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')


关键字