Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

fabric

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,


2 Answers

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

like image 135
Marcelo Cantos Avatar answered Sep 12 '25 22:09

Marcelo Cantos


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.

like image 45
Morgan Avatar answered Sep 12 '25 23:09

Morgan