Fabric run remote with specify user on a hostname

A little snippet to run fabric under specify user with ssh config:

from fabric.api import * # not really good, i dont like this kind of import

env.use_ssh_config = True # use configuration was define in ~/.ssh/config

@task
@hosts("host_name") # host_name is one you define in your ~/.ssh/config file
                    # this file will contain information about: ip, username, port, indentity file
def task_name():
    with settings(user="user_name"): # username is the user you want to login
                                     # if you dont set in in settings(user=<user_name>)
                                     # the default user will be get from env.user
        # run your task here

A example of ~/.ssh/config file

Host vagrant
HostName 127.0.0.1
User vagrant
IdentityFile ~/.ssh/id_rsa
Port 2222

Kien Nguyen Trung

A father, husband and Elixir lover.

Ho Chi Minh, Vietnam http://kiennt.com