user402250
user402250

Reputation:

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

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 each 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,

Upvotes: 25

Views: 17494

Answers (3)

Marcelo Cantos
Marcelo Cantos

Reputation: 185842

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

Upvotes: 40

Morgan
Morgan

Reputation: 4131

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.

Upvotes: 26

user402250
user402250

Reputation:

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.

Upvotes: 3

Related Questions