How to set target hosts in Fabric file

I do this by declaring an actual function for each environment. For example:

def test():
    env.user="testuser"
    env.hosts = ['test.server.com']

def prod():
    env.user="produser"
    env.hosts = ['prod.server.com']

def deploy():
    ...

Using the above functions, I would type the following to deploy to my test environment:

fab test deploy

…and the following to deploy to production:

fab prod deploy

The nice thing about doing it this way is that the test and prod functions can be used before any fab function, not just deploy. It is incredibly useful.

Leave a Comment