fabric different parameters in different hosts

fabric, python

Solution

The way I do this is to parametize the tasks. In my case it's about Deployment to Dev, Test and Production.

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

Hopefully this is obvious enough to see that you can configure all kinds of deployments by simply editing your `deploy.ini` configuration file and subsequently automatically ceacreating new tasks that match with the right parameters.

This pattern can be adapted to YAML or JSON if that's your "cup of tea'>

Problem

Please tell me how can i execute fab-script in a list of hosts with the same command BUT with the different values of parameter. Something like this: ``` from fabric.api import * def command(parameter): run ("command%s" % parameter) ``` and execute this. I dont now how. For example: fab -H host1,host2,host3 command:param1 command:param2 command:param3 And Fabric performs the following: - command:param1 executed on host1 - command:param2 executed on host2 - command:param3 executed on host3

Original source