python - 如何将自定义选项传递给 fab 命令

标签 python fabric

我想为多个项目重用 fabfile。

config.ini

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

fabfile.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 传递自定义参数吗?需要帮助。

最佳答案

当然,您可以将参数传递给从 invoke.task 调用屋顶任务的 fab 任务。 我将举例说明如何做到这一点: 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!

命名任务参数也是如此。

关于python - 如何将自定义选项传递给 fab 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52560969/

相关文章:

python - Pandas 填充: How to fill only leading NaN from beginning of series until first value appears?

python - 在python中将PNG转换为JPG

python-3.x - Python 3 对 Fabric 的支持

python - 直接在 Python 中调用 Fabric

python - 如何防止fabric form等待进程返回

python - 如何通过远程服务器上的fabric替换文件字符串?

Python:如果/Elif 两次问单个问题而不是回答后继续?

python - 使用 Ctrl+C 停止 Python Gtk.Application

python - 如何在 Python 中编写与 Wikipedia 中的示例不同的策略模式?

python - 通过 Python/Fabric 从命令行更改 Unix 密码