本文介绍了如何使Fabric执行遵循env.hosts列表顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下fabfile.py:

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")

我得到以下输出:

[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.

请注意,执行顺序与env.hosts规范不同.

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

为什么这种方式起作用?有没有办法使执行顺序与env.hosts列表中指定的顺序相同?

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

推荐答案

不保留env.hosts中的顺序的确切原因是可以指定要操作的主机的三个级别" --env.主机,命令行和每个功能-合并在一起.在第309 行的fabric/main.py中,您可以看到他们使用set()类型删除了三个可能的主机列表中的重复项.由于set()没有顺序,因此主机将以随机"顺序作为列表返回.

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.

这是一个很好的理由.这是从列表中删除重复项的非常有效的机制,对于结构而言,顺序无关紧要很重要.您要让Fabric在各种主机上执行一系列完全并行的原子操作.通过并行的原子动作的本质,顺序不会影响动作成功执行的能力.如果订单 did 很重要,那么就必须采用其他策略,而面料将不再是完成该工作的正确工具.

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.

这篇关于如何使Fabric执行遵循env.hosts列表顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 20:40