开发者

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

开发者 https://www.devze.com 2023-01-08 16:54 出处:网络
I\'ve got this code (foo and bar are local servers): env.hosts = [\'foo\', \'bar\'] def mytask(): print(env.hosts[0])

I've got this code (foo and bar are local servers):

env.hosts = ['foo', 'bar']

def mytask():
    print(env.hosts[0])

Which, of course prints foo every iteration.

As you probably know, Fabric iterates through the env.hosts list and executes mytask() on ea开发者_运维百科ch of them this way:

fab mytask

does

task is executed on foo

task is executed on bar

I'm looking for a way to get the current host in every iteration.

Thanks,


Use env.host_string. You can find a full list of env variables here.


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.


Thanks Marcelo.

If you want to actually use env.host_string (for concatenation purpose for instance), be sure to be inside a task. Its value is None outside.

0

精彩评论

暂无评论...
验证码 换一张
取 消