无法识别结构环境主机 [英] fabric env.hosts not being identified
问题描述
我有一个简单的fabfile,名称是 env_fabfile.py
I have a simple fabfile by the name env_fabfile.py
# env_fabfile.py
# makes use of fab env variables
from fabric.api import env, run
def login():
env.hosts = ['user@host1:1234', 'user@host2:2345']
env.passwords = {'user@host1:1234': 'pass1', 'user@host2:2345': 'pass2'}
env.parallel=True
def run_lsb_release():
run('lsb_release -a')
现在,我使用fab命令运行上述命令:
Now I run the above using the fab command as :
fab -f env_fabfile.py登录run_lsb_release
fab -f env_fabfile.py login run_lsb_release
它完美运行(并行)并提供所需的输出
And it runs perfectly (in parallel) and gives the desired output
现在,我想实际计算同一脚本以串行方式运行与并行运行之间的时间差.为此,我编写了以下python脚本: timecal.py
Now I wanted to actually calculate the time difference between when the same script is run in serial vs when it is run in parallel. So to do this I wrote the below python script: timecal.py
# timecal.py
# runs the fabfile once in serial and calculates the time
# then runs the same file in parallel and calculates the time
from fabric.api import env, run
import time
def login():
print "in login"
env.hosts = ['user@host1:1234', 'user@host2:2345']
env.passwords = {'user@host1:1234': 'pass1', 'user@host2:2345': 'pass2'}
def parallel(status):
print "in parallel"
env.parallel=status
def run_lsb_release():
print "in run"
run('lsb_release -a')
def do_serial():
start_time = time.time()
parallel(False)
login()
run_lsb_release()
elapsed_time = time.time() - start_time
return elapsed_time
def do_parallel():
start_time = time.time()
parallel(True)
login()
run_lsb_release()
elapsed_time = time.time() - start_time
return elapsed_time
if __name__ == '__main__':
print "Running in serial mode "
print "Total time taken ", do_serial()
print "Running in parallel mode"
print "Total time taken ", do_parallel()
但是当我以
python timecal.py
python timecal.py
我在stdout上获得了以下内容(除了代码中的打印语句)
I get the below on stdout (apart from the print statements in the code)
未找到主机.请指定连接字符串(单个):
No hosts found. Please specify (single) host string for connection:
我不明白为什么?还有如何纠正脚本,以便我可以实现想要的功能(如上面的问题所述)
I don't understand why ? Also how can the script be rectified so that I can achieve what I want to (as stated in the question above)
如果我尝试使用不同版本的timecal.py,例如:
In case I try a different version of timecal.py, as :
from fabric.api import run, settings, env
import time
def do_parallel():
start_time = time.time()
env.hosts = ['user@host1:1234', 'user@host2:2345']
env.passwords = {'user@host1:1234': 'pass1', 'user@host2:2345': 'pass2'}
env.parallel=True
run('lsb_release -a')
elapsed_time = time.time() - start_time
return elapsed_time
def do_serial():
start_time = time.time()
with settings(host_string='host1', port=1234, user='user', password='pass1'):
run('lsb_release -a')
with settings(host_string='host2', port=2345, user='user', password='pass2'):
run('lsb_release -a')
elapsed_time = time.time() - start_time
return elapsed_time
if __name__ == '__main__':
try:
print "Running in parallel mode"
print "Total time taken ", do_parallel()
print "Running in serial mode "
print "Total time taken ", do_serial()
except Exception as e:
print e
我收到以下错误:
致命错误:需要提示输入目标主机连接字符串(主机:无),但是在并行模式下输入将不明确
Fatal error: Needed to prompt for the target host connection string (host: None), but input would be ambiguous in parallel mode
我不明白主持人的原因:这里没有?代码有什么问题?
I don't understand why is the host: None here ? What is wrong with the code ?
推荐答案
简短的答案是,您不应该以当前的方式设置env.hosts
值,并且env.passowrds
过于粗略(也许断了?),并建议使用基于SSH密钥的访问方式,尤其是利用本机SSH配置文件.
The short answer is that you shouldn't set the env.hosts
value the way you are currently doing, and env.passowrds
is super-sketchy (broken maybe?), and it's recommended to use SSH key-based access, especially leveraging native SSH config files.
这是您的timecal.py脚本的修改版本,可以按预期工作,下面将介绍一些区别.
Here's the modified version of your timecal.py script which works as expected, and I'll call out some of the differences below.
# timecal.py
# runs the fabfile once in serial and calculates the time
# then runs the same file in parallel and calculates the time
from fabric.api import env, run, execute, parallel
import time
env.use_ssh_config = True
env.roledefs = {
"my_servers": ['server_1', 'server_2']
}
def run_lsb_release():
print "in run"
run('lsb_release -a')
def do_task(task_func):
start_time = time.time()
execute(task_func, roles=['my_servers'])
elapsed_time = time.time() - start_time
return elapsed_time
if __name__ == '__main__':
print "Running in serial mode "
print "Total time taken ", do_task(run_lsb_release)
print "Running in parallel mode"
print "Total time taken ", do_task(parallel(run_lsb_release))
主要区别是使用env.roledefs
和SSH配置文件,而不是主机&密码.由于这些任务是在单独的线程中执行的,因此这些值在并行执行模式下将不起作用. 文档有点薄,但这基本上就是为什么您要有这个问题.
The main difference is using env.roledefs
, and the SSH config file, rather than hosts & passwords. Those values will NOT work in the parallel execution mode, due to the fact that those tasks are executed in separate threads. The docs are a little thin, but that's basically why you're having this problem.
这篇关于无法识别结构环境主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!