How to set target hosts in Fabric file

后端 未结 15 786
南方客
南方客 2020-11-29 15:27

I want to use Fabric to deploy my web app code to development, staging and production servers. My fabfile:

def deploy_2_dev():
  deploy(\'dev\')

def deploy_         


        
15条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 16:05

    Since fab 1.5 this is a documented way to dynamically set hosts.

    http://docs.fabfile.org/en/1.7/usage/execution.html#dynamic-hosts

    Quote from the doc below.

    Using execute with dynamically-set host lists

    A common intermediate-to-advanced use case for Fabric is to parameterize lookup of one’s target host list at runtime (when use of Roles does not suffice). execute can make this extremely simple, like so:

    from fabric.api import run, execute, task
    
    # For example, code talking to an HTTP API, or a database, or ...
    from mylib import external_datastore
    
    # This is the actual algorithm involved. It does not care about host
    # lists at all.
    def do_work():
        run("something interesting on a host")
    
    # This is the user-facing task invoked on the command line.
    @task
    def deploy(lookup_param):
        # This is the magic you don't get with @hosts or @roles.
        # Even lazy-loading roles require you to declare available roles
        # beforehand. Here, the sky is the limit.
        host_list = external_datastore.query(lookup_param)
        # Put this dynamically generated host list together with the work to be
        # done.
        execute(do_work, hosts=host_list)
    

提交回复
热议问题