首页 > 解决方案 > 如何将自定义选项传递给 fab 命令

问题描述

我想为多个项目重用 fabfile。

配置文件

[project1]
git_repo = git@github/project1
project_path = '/path/project1'
[project2]
git_repo = git@github/project22
project_path = '/path/project2'

工厂文件.py

from fabric import task
config = configparser.ConfigParser()
config.read("conf.ini")

@task
def getcode(connection, project, git_repo):
    args = config['project]
    connection.run("git clone {}".format(git_repo))

@task
def pushcode(connection, project, git_repo):
    args = config['project]
    connection.run("git push {}".format(git_repo))

我怎样才能避免args = config['project]在每种方法中使用。我可以使用 fab 命令传递自定义参数吗fab -H web1 --project=project1 pushcode?需要帮忙。

标签: pythonfabric

解决方案


当然,您可以将参数传递给从invoke.task. 我将举例说明如何做到这一点: fabfile.py

from fabric import task

@task
def sampleTask(connection, name, laste_name, age):
    print("The firstname is ", name)
    print("The lastname is ", laste_name)
    print("The age is ", age)

然后你像这样从命令行调用它: 命令行

fab sampleTask -n peshmerge -l Mo -a 28

输出应该是这样的:

[vagrant@localhost fabric]$ fab sampleTask -n peshmerge -l Mo -a 28
The firstname is Peshmerge
The lastname is Mo
The age is 28

注意:为您的任务命名包含下划线 (_) 将导致错误

No idea what 'sample_task' is!

命名任务参数也是如此。


推荐阅读