简体   繁体   English

使用Python Fabric Library获取env.hosts列表的当前值

[英]Get the current value of env.hosts list with Python Fabric Library

I've got this code ( foo and bar are local servers): 我有以下代码( foobar是本地服务器):

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 taskfoo上执行
task is executed on bar taskbar上执行

I'm looking for a way to get the current host in every iteration. 我正在寻找一种在每次迭代中获取当前主机的方法。

Thanks, 谢谢,

Use env.host_string . 使用env.host_string You can find a full list of env variables here . 您可以在此处找到env变量的完整列表。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM