请告诉我如何在具有相同命令BUT的主机列表中执行fab-script并使用不同的参数值。 像这样:
from fabric.api import *
def command(parameter):
run ("command%s" % parameter)
并执行此操作。我现在不怎么样。例如:
fab -H host1,host2,host3命令:param1命令:param2命令:param3
Fabric将执行以下操作:
答案 0 :(得分:3)
我这样做的方法是参数化任务。就我而言,它是关于部署到开发,测试和生产。
<强> fabfile.py:强>
from ConfigParser import ConfigParser
from fabric.tasks import Task
from fabric.api import execute, task
@task()
def build(**options):
"""Your build function"""
class Deploy(Task):
name = "dev"
def __init__(self, *args, **kwargs):
super(Deploy, self).__init__(*args, **kwargs)
self.options = kwargs.get("options", {})
def run(self, **kwargs):
options = self.options.copy()
options.update(**kwargs)
return execute(build, **options)
config = ConfigParser()
config.read("deploy.ini")
sections = [
section
for section in config.sections()
if section != "globals" and ":" not in section
]
for section in sections:
options = {"name": section}
options.update(dict(config.items("globals")))
options.update(dict(config.items(section)))
t = Deploy(name=section, options=options)
setattr(t, "__doc__", "Deploy {0:s} instance".format(section))
globals()[section] = task
<强> deploy.ini:强>
[globals]
repo = https://github.com/organization/repo
[dev]
dev = yes
host = 192.168.0.1
domain = app.local
[prod]
version = 1.0
host = 192.168.0.2
domain = app.mydomain.tld
希望这很明显,您可以通过简单地编辑deploy.ini
配置文件并随后自动终止与正确参数匹配的新任务来配置所有类型的部署。