基础一:
-
#!/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模块