Search code examples
pythontaskfabricexecute

How to Pass Fabric Task name as Variable to execute()


I have a situation where the name of the task to be executed comes in from a json as a String. More like u'taskName'

I am trying to execute tasks in this format:

import fabfile tn = mydict['taskname'] # this taskname is definitely present in the fabfile imported. //do something so this works: out = execute(tn) # This obviously doesnt work!

I am aware that execute takes in a Task object.

Can someone help me in converting the string (in tn) into a Task object / or if my statement is wrong, Can someone help me in figuring out how to call that particular task present in the fabfile please?


Solution

  • If you're receiving errors your execute statement called an object that isn't a function of used a string that isn't a task name. See this example for how this can work simply:

    from fabric.api import *

    @task
    def foo():
        print "bar"
    
    
    @task
    def buzz():
        execute("foo")
        execute(foo)
    

    Note that this is from the docs of execute, that it will take a string (as long as that's a @task decorated function) or the function object (no decoration needed).

    Runs like this:

    $ fab -f test.py buzz
    bar
    bar
    
    Done.
    

    Also note that you don't get the output of the run from an execute() call. See again the docs section on the return value which is "a dictionary mapping host strings to the given task’s return value for that host’s execution run."