tags:

views:

142

answers:

1

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?

+4  A: 

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.

Travis Bradshaw
Thanks Travis, I like your answer. :) The execution order doesn't really matter in my case. I'm just curious about this behavior.
Wang Dingwei
Glad that I could help. One of my favorite tips is to make special note of the [Managing Output][1] section of the documentation. Once you get a series of commands going out to a large fleet of machines, it can be hard to check up on the output. I was able to remove a lot of noise with those features. [1] http://docs.fabfile.org/0.9.0/usage/output_controls.html
Travis Bradshaw