基本python代码:
for host in hosts:
with settings(
host=host,
user=user,
password=passwd,`enter code here`
):
run('uname -a')
我想改变如下:
def take_with_out(host, user, password):
with settings(
host=host,
user=user,
password=passwd):
def dec(fn):
def wrapper(*argv, **kwgs):
fn(*argv, **kwgs)
return wrapper
return dec
@take_with_out(host, user, password)
def foo(command):
run(command)
foo("uname -a")
错误消息是要显示我键入主机以确保将s继承到ssh主机,而基本代码不会询问主机,但更改的代码应该。
答案 0 :(得分:1)
试试这段代码:
class add_context(object):
def __init__(self, host, user, password):
self.host = host
self.user = user
self.password = password
def __call__(self, function):
def wrapped_function(*args,**kwargs):
with settings(host=self.host, user=self.user, password=self.password):
return function(*args, **kwargs)
return wrapped_function
@add_context("localhost","unknow","password")
def foo(command):
run(command)
foo("uname -a")