【python】python 模块学习之--Fabric

基础一:

  1. #!/usr/bin/env python
  2. from fabric.api import *

  3. env.user='root'
  4. env.hosts=['218.78.186.162','125.208.12.56']
  5. env.passwords={ '[email protected]:22':'XXX','[email protected]:22':'XXXX@0'}

  6. @runs_once                       ####runs_once代表只执行一次
  7. def local_task():
  8.     local("hostname")            ####local本地任务,不会ssh远程执行

  9. def remote_task():
  10.     with cd("/tmp/"):
  11.         run("hostname")          ###run 远程命令

  12. @task                            ####task标记只有go函数可以调用remote_task函数
  13. def go():
  14.     remote_task()

测试
  1. [root@hostnfsd :/soft/python/pyauto/第七章/fabric]$ fab -f simple1_test.py remote_task    ###直接调用remote_task函数失败

  2. Warning: Command(s) not found:
  3.     remote_task

  4. Available commands:

  5.     go
  6. [root@hostnfsd :/soft/python/pyauto/第七章/fabric]$ fab -f simple1_test.py local_task   ###有task表标识时直接调用local函数失败,meitask时才能直接调用local函数

  7. Warning: Command(s) not found:
  8.     local_task

  9. Available commands:

  10.     go
  11. [root@hostnfsd :/soft/python/pyauto/第七章/fabric]$ fab -f simple1_test.py go         通过go函数调用remote_task函数
  12. [218.78.186.162] Executing task 'go'
  13. [218.78.186.162] run: hostname
  14. [218.78.186.162] out: localhost.localdomain
  15. [218.78.186.162] out:

  16. [125.208.12.56] Executing task 'go'
  17. [125.208.12.56] run: hostname
  18. [125.208.12.56] out: host-192-168-1-56
  19. [125.208.12.56] out:


  20. Done.
  21. Disconnecting from 218.78.186.162... done.
  22. Disconnecting from 125.208.12.56... done.


有时我们希望直接用脚本就可以执行,可以如下更改
  1. #!/usr/bin/env python
  2. from fabric.api import *

  3. env.user='root'
  4. env.hosts=['218.78.186.162','125.208.12.56']
  5. env.passwords={ '[email protected]:22':'ESBecs00','[email protected]:22':'eRaMUnA612@0'}

  6. @runs_once
  7. def local_task():
  8.     local("hostname")

  9. def remote_task():
  10.     with cd("/tmp/"):
  11.         run("hostname")



  12. def go():
        execute(remote_task)           ####execute表示在脚本内执行即可
  13.     execute(local_task)
    go()
直接运行即可
[root@hostnfsd :/soft/python/pyauto/第七章/fabric]$ python simple1_test.py



基础2:
  1. #!/usr/bin/env python
  2. from fabric.api import *

  3. env.user='root'
  4. env.hosts=['218.78.186.162','125.208.12.56']
  5. env.passwords={ '[email protected]:22':'XXX','[email protected]:22':'XXXX@0'}

  6. @runs_once
  7. def input_raw():
  8.     return prompt("please input directory name:",default="/home")

  9. def worktask(dirname):
  10.     run("ls -l "+dirname)

  11. @task
  12. def go():
  13.     getdirname = input_raw()
  14.     worktask(getdirname)


跳板机:
  1. #!/usr/bin/env python
  2. from fabric.api import *
  3. from fabric.context_managers import *
  4. from fabric.contrib.console import confirm

  5. env.user='root'
  6. env.gateway='218.78.186.162'
  7. env.hosts=['125.208.12.56']
  8. env.passwords={ '[email protected]:22':'XX','[email protected]:22':'XXXX@0'}



  9. lpackpath="/home/install/lnmp0.9.tar.gz"
  10. rpackpath="/tmp/install"

  11. @task
  12. def put_task():
  13.     run("mkdir -p /tmp/install")
  14.     with settings(warn_only=True):
  15.         result = put(lpackpath, rpackpath)
  16.     if result.failed and not confirm("put file failed, Continue[Y/N]?"):
  17.         abort("Aborting file put task!")

  18. @task
  19. def run_task():
  20.     with cd("/tmp/install"):
  21.         run("tar -zxvf lnmp0.9.tar.gz")
  22.         run("ls -l")

  23. @task
  24. def go():
  25.     put_task()
  26.     run_task()


有时需要将这些功能模板写到django中,那么我们可以将该功能封装到一个类中
#!/usr/bin/env python
from fabric.api import *
class Student(object):
        def __init__(self,user,ip):
                env.user=user
                env.hosts=[ip]
                env.password='XXX'
        @runs_once
        def local_task(self):
                local("hostname")

        def remote_task(self):
                vhost=run("df -h")
                return vhost

def yunxing(user,ip):
    tom=Student(user,ip)
    print execute(tom.remote_task)


yunxing('root','218.78.186.162')   ###直接调用该函数传参即可


更多内容可见《python自动化运维技术与最佳实 》 第7章-Fabric模块

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29096438/viewspace-2124011/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29096438/viewspace-2124011/

你可能感兴趣的:(【python】python 模块学习之--Fabric)