Question

I have the following fabfile.py:

from fabric.api import env, run

host1 = '192.168.200.181'
host2 = '192.168.200.182'
host3 = '192.168.200.183'

env.hosts = [host1, host2, host3]

def df_h():
    run("df -h | grep sda3")

And I get the following output:

[192.168.200.181] run: df -h | grep sda3
[192.168.200.181] out: /dev/sda3             365G  180G  185G  50% /usr/local/nwe
[192.168.200.183] run: df -h | grep sda3
[192.168.200.183] out: /dev/sda3             365G   41G  324G  12% /usr/local/nwe
[192.168.200.182] run: df -h | grep sda3
[192.168.200.182] out: /dev/sda3             365G   87G  279G  24% /usr/local/nwe

Done.
Disconnecting from 192.168.200.182... done.
Disconnecting from 192.168.200.181... done.
Disconnecting from 192.168.200.183... done.

Note that the execution order is different from the env.hosts specification.

Why does it work this way? Is there a way to make the execution order the same as specified in env.hosts list?

Was it helpful?

Solution

The exact reason that the order is not preserved from env.hosts is that there are three "levels" that the hosts to operate can be specified--env.hosts, the command line, and per function--which are merged together. In fabric/main.py on line 309, you can see that they use the set() type to remove duplicates in the three possible lists of hosts. Since set() does not have an order, the hosts will be returned as a list in "random" order.

There's a pretty good reason that this is method. It's a very efficient mechanism for removing duplicates from a list and for fabric it's important that order doesn't matter. You're asking fabric to perform a series of completely parallel, atomic actions on various hosts. By the very nature of parallel, atomic actions, order does not effect the ability of the actions to be performed successfully. If order did matter, then a different strategy would be necessary and fabric would no longer be the correct tool for the job.

That said, is there a particular reason that you need these operations to occur in order? Perhaps if you're having some sort of problem that's a result of execution order, we can help you work that out.

OTHER TIPS

Just to update, newest Fabric 1.1+ (think even 1.0) dedupes in an order preserving way now. So this should be a non-issue now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top