如何使一些Fabric任务仅在本地运行一次,而其他任务在所有主机上运行(How to have some Fabric tasks only run once locally while others run on all hosts)

在我的结构脚本中,我遇到以下问题。 我有一个称为autodeploy的主要任务。 在这个任务中,我有一些我只想在本地运行一次的任务。 所有远程任务都应该在主机列表的每个主机上运行。

env.roledefs ={ 'testing': ['t-server-01', 't-server-02'] 'staging': ['s-server-01', 's-server-02'] 'live': ['l-server-01', 'l-server-02'] } def localtask1(): # download artifact def localtask2(): # cleanup locally def remotetask(): # deploy artifact to all hosts def autodeploy(): localtask1() # run this task only once, locally remotetask() # run this task on all hosts localtask2() # run this task only once

电话如下。 我想将角色作为属性传递。

fab -R test autodeploy

In my fabric scripts I have the following problem. I have a main task called autodeploy. Within this task I have some tasks that I only want to run once, locally. all remote tasks should run on each of the hosts of the host list.

env.roledefs ={ 'testing': ['t-server-01', 't-server-02'] 'staging': ['s-server-01', 's-server-02'] 'live': ['l-server-01', 'l-server-02'] } def localtask1(): # download artifact def localtask2(): # cleanup locally def remotetask(): # deploy artifact to all hosts def autodeploy(): localtask1() # run this task only once, locally remotetask() # run this task on all hosts localtask2() # run this task only once

The call is the following. I want to pass the role as an attribute.

fab -R test autodeploy

最满意答案

使用包装函数autodeploy中的execute函数,并为远程任务指定主机列表。

对于另外两个,您可以使用execute来调用它们,例如远程任务或直接调用它们。 使用它们中的本地函数,你会很好,并且不需要在本地主机上有ssh。

文档在这里是为了最好的使用新的执行功能 。

编辑

既然你在评论中提到了一个不同的用例,我会模拟你将如何做到这一点,从已经提供的文档中的位,添加参数传递部分

码:

#copy above #redefine this one def autodeploy(role_from_arg): localtask1() execute(remotetask, role=role_from_arg) localtask2() #calls like fab autodeploy:testing

Use the execute function inside the wrapper function autodeploy, and specify a host list for the remote task.

For the other two you can call them with execute, like for the remote task, or directly. Use the local function inside them and you'll be fine, and not need to have ssh on localhost.

Docs are here for how best ot use the new execute function.

EDIT

Since you mention a different use case in the comments I'll mock up how you'd do that, from bits in the documentation given already, adding the param passing section

code:

#copy above #redefine this one def autodeploy(role_from_arg): localtask1() execute(remotetask, role=role_from_arg) localtask2() #calls like fab autodeploy:testing

更多推荐