问题:将参数传递给结构任务

从命令行调用“ fab”时,如何将参数传递给Fabric任务?例如:

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

Done.

是否可以在没有提示的情况下执行此操作fabric.operations.prompt

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

Done.

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


回答 0

Fabric 2任务参数文档:

http://docs.pyinvoke.org/zh_CN/latest/concepts/invoking-tasks.html#task-command-line-arguments


Fabric 1.X使用以下语法将参数传递给任务:

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

您可以在Fabric文档中阅读有关它的更多信息。

Fabric 2 task arguments documentation:

http://docs.pyinvoke.org/en/latest/concepts/invoking-tasks.html#task-command-line-arguments


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.


回答 1

结构参数是通过非常基本的字符串解析来理解的,因此您在发送它们时必须要小心一点。

以下是将参数传递给以下测试函数的几种不同方式的示例:

@task
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'})

我在这里使用双引号将外壳排除在等式之外,但对于某些平台,单引号可能更好。还要注意Fabric认为是定界符的字符的转义符。

docs中有更多详细信息:http : //docs.fabfile.org/en/1.14/usage/fab.html#per-task-arguments

Fabric 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:

@task
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: http://docs.fabfile.org/en/1.14/usage/fab.html#per-task-arguments


回答 2

在Fabric 2中,只需将参数添加到任务函数即可。例如,将version参数传递给task deploy

@task
def deploy(context, version):
    ...

如下运行:

fab -H host deploy --version v1.2.3

Fabric甚至自动记录选项:

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

Docstring:
  none

Options:
  -v STRING, --version=STRING

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

@task
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 ...]

Docstring:
  none

Options:
  -v STRING, --version=STRING

回答 3

您需要将所有Python变量作为字符串传递,尤其是在使用子进程来运行脚本时,否则会出错。您将需要分别将变量转换回int / boolean类型。

def print_this(var):
    print str(var)

fab print_this:'hello world'
fab print_this='hello'
fab print_this:'99'
fab print_this='True'

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'

回答 4

如果有人希望将参数从一个任务传递给fabric2中的另一个任务,则只需使用环境字典即可:

@task
def qa(ctx):
  ctx.config.run.env['counter'] = 22
  ctx.config.run.env['conn'] = Connection('qa_host')

@task
def sign(ctx):
  print(ctx.config.run.env['counter'])
  conn = ctx.config.run.env['conn']
  conn.run('touch mike_was_here.txt')

并运行:

fab2 qa sign

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

@task
def qa(ctx):
  ctx.config.run.env['counter'] = 22
  ctx.config.run.env['conn'] = Connection('qa_host')

@task
def sign(ctx):
  print(ctx.config.run.env['counter'])
  conn = ctx.config.run.env['conn']
  conn.run('touch mike_was_here.txt')

And run:

fab2 qa sign

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。