How to set target hosts in Fabric file How to set target hosts in Fabric file python python

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.


Use roledefs

from fabric.api import env, runenv.roledefs = {    'test': ['localhost'],    'dev': ['user@dev.example.com'],    'staging': ['user@staging.example.com'],    'production': ['user@production.example.com']} def deploy():    run('echo test')

Choose role with -R:

$ fab -R test deploy[localhost] Executing task 'deploy'...


Here's a simpler version of serverhorror's answer:

from fabric.api import settingsdef mystuff():    with settings(host_string='192.0.2.78'):        run("hostname -f")