Using an SSH keyfile with Fabric Using an SSH keyfile with Fabric python python

Using an SSH keyfile with Fabric


Finding a simple fabfile with a working example of SSH keyfile usage isn't easy for some reason. I wrote a blog post about it (with a matching gist).

Basically, the usage goes something like this:

from fabric.api import *env.hosts = ['host.name.com']env.user = 'user'env.key_filename = '/path/to/keyfile.pem'def local_uname():    local('uname -a')def remote_uname():    run('uname -a')

The important part is setting the env.key_filename environment variable, so that the Paramiko configuration can look for it when connecting.


Also worth mentioning here that you can use the command line args for this:

fab command -i /path/to/key.pem [-H [user@]host[:port]]


Another cool feature available as of Fabric 1.4 - Fabric now supports SSH configs.

If you already have all the SSH connection parameters in your ~/.ssh/config file, Fabric will natively support it, all you need to do is add:

env.use_ssh_config = True

at the beginning of your fabfile.