【python】python 模組學習之--Fabric
基礎一:
-
#!/usr/bin/env python
-
from fabric.api import *
-
-
env.user='root'
-
env.hosts=['218.78.186.162','125.208.12.56']
-
env.passwords={ 'root@218.78.186.162:22':'XXX','root@125.208.12.56:22':'XXXX@0'}
-
-
@runs_once ####runs_once代表只執行一次
-
def local_task():
-
local("hostname") ####local本地任務,不會ssh遠端執行
-
-
def remote_task():
-
with cd("/tmp/"):
-
run("hostname") ###run 遠端命令
-
-
@task ####task標記只有go函式可以呼叫remote_task函式
-
def go():
- remote_task()
測試
-
[root@hostnfsd :/soft/python/pyauto/第七章/fabric]$ fab -f simple1_test.py remote_task ###直接呼叫remote_task函式失敗
-
-
Warning: Command(s) not found:
-
remote_task
-
-
Available commands:
-
-
go
-
[root@hostnfsd :/soft/python/pyauto/第七章/fabric]$ fab -f simple1_test.py local_task ###有task表標識時直接呼叫local函式失敗,meitask時才能直接呼叫local函式
-
-
Warning: Command(s) not found:
-
local_task
-
-
Available commands:
-
-
go
-
[root@hostnfsd :/soft/python/pyauto/第七章/fabric]$ fab -f simple1_test.py go 透過go函式呼叫remote_task函式
-
[218.78.186.162] Executing task 'go'
-
[218.78.186.162] run: hostname
-
[218.78.186.162] out: localhost.localdomain
-
[218.78.186.162] out:
-
-
[125.208.12.56] Executing task 'go'
-
[125.208.12.56] run: hostname
-
[125.208.12.56] out: host-192-168-1-56
-
[125.208.12.56] out:
-
-
-
Done.
-
Disconnecting from 218.78.186.162... done.
- Disconnecting from 125.208.12.56... done.
有時我們希望直接用指令碼就可以執行,可以如下更改
-
#!/usr/bin/env python
-
from fabric.api import *
-
-
env.user='root'
-
env.hosts=['218.78.186.162','125.208.12.56']
-
env.passwords={ 'root@218.78.186.162:22':'ESBecs00','root@125.208.12.56:22':'eRaMUnA612@0'}
-
-
@runs_once
-
def local_task():
-
local("hostname")
-
-
def remote_task():
-
with cd("/tmp/"):
-
run("hostname")
-
-
-
-
def go():
execute(remote_task) ####execute表示在指令碼內執行即可 -
execute(local_task)
go()
[root@hostnfsd :/soft/python/pyauto/第七章/fabric]$ python simple1_test.py
基礎2:
-
#!/usr/bin/env python
-
from fabric.api import *
-
-
env.user='root'
-
env.hosts=['218.78.186.162','125.208.12.56']
-
env.passwords={ 'root@218.78.186.162:22':'XXX','root@125.208.12.56:22':'XXXX@0'}
-
-
@runs_once
-
def input_raw():
-
return prompt("please input directory name:",default="/home")
-
-
def worktask(dirname):
-
run("ls -l "+dirname)
-
-
@task
-
def go():
-
getdirname = input_raw()
- worktask(getdirname)
跳板機:
-
#!/usr/bin/env python
-
from fabric.api import *
-
from fabric.context_managers import *
-
from fabric.contrib.console import confirm
-
-
env.user='root'
-
env.gateway='218.78.186.162'
-
env.hosts=['125.208.12.56']
-
env.passwords={ 'root@218.78.186.162:22':'XX','root@125.208.12.56:22':'XXXX@0'}
-
-
-
-
lpackpath="/home/install/lnmp0.9.tar.gz"
-
rpackpath="/tmp/install"
-
-
@task
-
def put_task():
-
run("mkdir -p /tmp/install")
-
with settings(warn_only=True):
-
result = put(lpackpath, rpackpath)
-
if result.failed and not confirm("put file failed, Continue[Y/N]?"):
-
abort("Aborting file put task!")
-
-
@task
-
def run_task():
-
with cd("/tmp/install"):
-
run("tar -zxvf lnmp0.9.tar.gz")
-
run("ls -l")
-
-
@task
-
def go():
-
put_task()
- 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/29519108/viewspace-2134939/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python學習之模組Python
- 【python】python 模組學習之--pexpectPython
- Python學習之常用模組Python
- python學習之argparse模組Python
- Python學習之 datetime模組Python
- Python學習之模組與包Python
- Python學習之如何引用Python自定義模組?Python
- [Python模組學習] glob模組Python
- python開發學習之如何更好的引用Python模組?Python
- Python學習——xml模組PythonXML
- Python time模組學習Python
- python模組學習:CookiePythonCookie
- Python模組學習:atexitPython
- Python模組學習:fileinputPython
- Python學習——hashlib模組Python
- Python學習——shelve模組Python
- python 各種模組學習Python
- python optparse模組學習薦Python
- Python模組學習:urllibPython
- Python模組學習:datetimePython
- 小豬的Python學習之旅 —— 12.Python併發之queue模組Python
- python基礎學習16—-模組Python
- 學習Python的urllib模組Python
- Python學習——configparser模組Python
- python模組學習:anydbm, shelvePython
- python模組之collections模組Python
- python之模組Python
- python三大神器之fabricPython
- Python之numpy學習Python
- python之pandas學習Python
- Python之Series 學習Python
- python學習-fabric(高效遠端自動化部署工具)Python
- Python的shutil zipfile tarfile模組學習Python
- Python學習——logging模組Python
- Python模組學習:hashlib hash加密Python加密
- Python模組學習:copy 物件拷貝Python物件
- Python模組學習:subprocess 建立子程式Python
- Python學習筆記|Python之程式Python筆記