[英]Get the current value of env.hosts list with Python Fabric Library
I've got this code ( foo
and bar
are local servers): 我有以下代码(
foo
和bar
是本地服务器):
env.hosts = ['foo', 'bar']
def mytask():
print(env.hosts[0])
Which, of course prints foo every iteration. 当然,每次迭代都会打印foo 。
As you probably know, Fabric iterates through the env.hosts list and executes mytask() on each of them this way: 如您所知,Fabric遍历env.hosts列表,并通过以下方式对它们中的每一个执行mytask():
fab mytask
does 确实
task
is executed on foo
task
在foo
上执行
task
is executed on bar
task
在bar
上执行
I'm looking for a way to get the current host in every iteration. 我正在寻找一种在每次迭代中获取当前主机的方法。
Thanks, 谢谢,
You can just do: 您可以这样做:
env.hosts = ['foo', 'bar']
def mytask():
print(env.host)
Because when you're in the task as executed by fab, you'll have that var set for free. 因为当您执行由fab执行的任务时,将免费设置该变量。
Thanks Marcelo. 谢谢马塞洛。
If you want to actually use env.host_string
(for concatenation purpose for instance), be sure to be inside a task. 如果要实际使用
env.host_string
(例如用于串联目的),请确保位于任务内部。 Its value is None
outside. 其值是“
None
外部”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.