Execute fabric task for specific host only once

时间:2015-06-25 18:53:45

标签: python fabric

Im trying to execute main task that needs to execute tasks differently for each host. In the following setup, task 'sometask' will get execute twice for each host. What is the best way to prevent that? @task @hosts('host1', 'host2') def test(): execute(do_something_everywhere) # execute on both hosts execute(sometask, 'arg1', host='host1') # execute on host1 only execute(sometask, 'arg2', host='host2') # execute on host2 only

1 个答案:

答案 0 :(得分:0)

您可以使用select * from my_table inner join ( select id, max(ref_value) max_value from my_table group by id ) t on t.id = my_table_id, t.max_vale = my_table_value 装饰器来解决这个问题,但我发现这可能导致额外的工作使得包装器函数获得您想要的执行顺序,所以这里有一个快速修复,使用@runs_once值来评估您要部署到哪个服务器并相应地调整脚本:

env.host_string

此输出中的结果似乎达到了您想要的效果:

@hosts('host1', 'host2')
@task
def checkout(branch='master'):
    execute(_test_task_w_arg, 'all-servers')
    execute(_test_task_w_arg, 'arg1' if env.host_string == 'host1' else 'arg2')

def _test_task_w_arg(arg1):
    local("touch test-file-" + arg1)