Different fabric parameters in different hosts

Please tell me how I can execute fab-script in the list of hosts with the same command, BUT with different parameter values. Something like that:

from fabric.api import *

def command(parameter):
        run ("command%s" % parameter)

and do it. I do not know how to do that. For instance:

fab -H host1, host2, host3 command: param1 command: param2 command: param3

And Fabric does the following:

  • command: param1 is executed on host1
  • command: param2 runs on host2
  • command: param3 runs on host3
+4
source share
1 answer

The way I do this is to parameterize tasks. In my case, it's about deploying 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

, , , , deploy.ini , .

YAML JSON, " " >

+3

Source: https://habr.com/ru/post/1548635/


All Articles