Pass parameter to fabric task


How can I pass a parameter to a fabric task when calling “fab” from the command line? For example:

def task(something=''):
    print "You said %s" % something
$ fab task "hello"
You said hello


Is it possible to do this without prompting with fabric.operations.prompt?

Asked By: Donovan



Fabric 2 task arguments documentation:

Fabric 1.X uses the following syntax for passing arguments to tasks:

 fab task:'hello world'
 fab task_something='hello'
 fab task_foo=99,bar=True
 fab task:foo,bar

You can read more about it in Fabric docs.

Answered By: Jakub Roztocil

You need to pass all Python variables as strings, especially if you are using sub-process to run the scripts, or you will get an error. You will need to convert the variables back to int/boolean types separately.

def print_this(var):
    print str(var)

fab print_this:'hello world'
fab print_this='hello'
fab print_this:'99'
fab print_this='True'
Answered By: frage

Fabric 1.x arguments are understood with very basic string parsing, so you have to be a bit careful with how you send them.

Here are a few examples of different ways to pass arguments to the following test function:

def test(*args, **kwargs):
    print("args:", args)
    print("named args:", kwargs)

$ fab "test:hello world"
('args:', ('hello world',))
('named args:', {})

$ fab "test:hello,world"
('args:', ('hello', 'world'))
('named args:', {})

$ fab "test:message=hello world"
('args:', ())
('named args:', {'message': 'hello world'})

$ fab "test:message=message = hello, world"
('args:', ())
('named args:', {'message': 'message = hello, world'})

I use double quote here to take the shell out of the equation, but single quotes may be better for some platforms. Also note the escapes for characters that fabric considers delimiters.

More details in the docs:

Answered By: Bogdan Kiselitsa

If someone is looking to pass parameters from one task to another in fabric2, just use the environment dictionary for that:

def qa(ctx):['counter'] = 22['conn'] = Connection('qa_host')

def sign(ctx):
  conn =['conn']'touch mike_was_here.txt')

And run:

fab2 qa sign
Answered By: MikeL

In Fabric 2, simply add the argument to your task function. For example, to pass the version argument to task deploy:

def deploy(context, version):

Run it as follows:

fab -H host deploy --version v1.2.3

Fabric even documents the options automatically:

$ fab --help deploy
Usage: fab [--core-opts] deploy [--options] [other tasks here ...]


  -v STRING, --version=STRING
Answered By: mrts
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.