Skip to content

Instantly share code, notes, and snippets.

@jywsn
Last active August 29, 2015 14:10
Show Gist options
  • Save jywsn/7ba7d71c4e186a38028d to your computer and use it in GitHub Desktop.
Save jywsn/7ba7d71c4e186a38028d to your computer and use it in GitHub Desktop.
Demonstration of fabric's hosts and roles when using execute()
"""
About fabric's hosts, roles, and execute()
See http://docs.fabfile.org/en/1.10/usage/execution.html
Per-task, command line roles will override role decorators:
fab setup testa:roles="a"
The testa() task is run once for each 'a' server.
fab setup testa:role="c"
The testa() task is run once for each 'c' server.
Per-task, command line hosts will override role decorators:
fab setup testa:hosts="server-a1"
The testa() task is run on server-a1.
Calling a task function with execute() is effectively the same as calling the
given task from the command line. If used inside a 'regular' task that is
going to run on multiple hosts, calls to execute will also run multiple times,
resulting in multiplicative numbers of subtask calls.
fab setup test:role="a"
The test() task will be run once for every 'a' server. BUT, each time test()
is executed, testa() will be executed for every 'a' and 'b' server, and testb()
will be executed for every 'a' and 'b' server.
"""
from fabric.api import env, put, run, local, settings, hide
from fabric.tasks import execute
from fabric.decorators import roles, task
@task
@roles('a', 'b')
def testa():
pass
@task
@roles('a', 'b')
def testb():
pass
@task
def test():
execute(testa)
execute(testb)
@task
def setup():
env.roledefs = {
'a': ['server-a1', 'server-a2'],
'b': ['server-b1', 'server-b2'],
'c': ['server-c1', 'server-c2']
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment