Can a Python Fabric task invoke other tasks and respect their hosts lists?

Since Fabric 1.3, the execute helper is now available to do just this. The documentation is available here: Intelligently executing tasks with execute.

Here is the example they use:

from fabric.api import run, roles, execute

env.roledefs = {
    'db': ['db1', 'db2'],
    'web': ['web1', 'web2', 'web3'],
}

@roles('db')
def migrate():
    # Database stuff here.
    pass

@roles('web')
def update():
    # Code updates here.
   pass

And then to run both migrate and web from another task deploy:

def deploy():
    execute(migrate)
    execute(update)

And this will respect the roles and hosts lists that those tasks have.

Leave a Comment

tech