【python】python 模組學習之--Fabric

studywell發表於2017-03-08

基礎一:

  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={ 'root@218.78.186.162:22':'XXX','root@125.208.12.56: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={ 'root@218.78.186.162:22':'ESBecs00','root@125.208.12.56: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={ 'root@218.78.186.162:22':'XXX','root@125.208.12.56: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={ 'root@218.78.186.162:22':'XX','root@125.208.12.56: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中,那麼我們可以將該功能封裝到一個類中
  1. #!/usr/bin/env python
  2. from fabric.api import *
  3. class Student(object):
  4.         def __init__(self,user,ip):
  5.                 env.user=user
  6.                 env.hosts=[ip]
  7.                 env.password='XXX'
  8.         @runs_once
  9.         def local_task(self):
  10.                 local("hostname")

  11.         def remote_task(self):
  12.                 vhost=run("df -h")
  13.                 return vhost

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


  17. yunxing('root','218.78.186.162')   ###直接呼叫該函式傳參即可


更多內容可見《python自動化運維技術與最佳實》 第7章-Fabric模組

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29519108/viewspace-2134939/,如需轉載,請註明出處,否則將追究法律責任。

相關文章