fab-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Fab-user] Using SSH Config/Host aliases


From: Carlton Gibson
Subject: [Fab-user] Using SSH Config/Host aliases
Date: Thu, 16 Dec 2010 15:09:06 +0000

Hi All, 

I'm getting started with Fabric and am trying to work out how best to work with it in regards to my ssh config details. 

As is common, I have a ~/.ssh/config file with named blocks for Hostnames, Ports, Identity files and so on. In the first place I'd like to simply reference those named blocks in my fabfile and have it use those to determine the right details directly. I appreciate that this isn't currently supported but is (possibly) scheduled for the 1.0 or 1.1 release.

In the meantime I've been experimenting with putative solutions that Google throws up (see below) and this brings me to my second (and main) desire/want. It's obviously not ideal but, I don't really mind duplicating the info in my ssh config file file while I wait for/lend a hand in getting Fabric to use it. What I do want to be able to do is specify rememberable/readable shortcut names for hosts in the fabfile and on the command line rather than opaque IP address & port number combinations. 

So, on the command line I'd like something like: 

$ fab -H my_site deploy

and in @hosts decorators I'd like something like: 

@hosts('my_site')

Even if the actual hostname is more like:

address@hidden:1234

I've put the necessary details in a dictionary with the nicknames as keys, which gives simple @hosts usage (at the cost of some duplication) but it doesn't get me the command line case. 

I guess I'm wondering how people handle this currently, and if there is any work that I can perhaps lend an eye to for implementing a solution here?


Below is just an example Google threw up parsing the ssh config file directly; however, it breaks the @hosts usage, which isn't ideal...

####### Extract from fabfile.py

env.hosts = ['my_site']

# An Example (first pass) solution found via Google...
#
# This parses my .ssh/config for the right information and updates env.hosts & env.key_filename accordingly.
# -- Trouble is it stops @hosts decorator using my preferred nicknames.
#
def annotate_hosts_with_ssh_config_info():
    def hostinfo(host, config):
        hive = config.lookup(host)
        host = hive['hostname']
        if 'user' in hive:
            host = 'address@hidden' % (hive['user'], host)
        if 'port' in hive:
            host = '%s:%s' % (host, hive['port'])
        return host

    try:
        config_file = file(expanduser('~/.ssh/config'))
    except IOError:
        pass
    else:
        config = SSHConfig()
        config.parse(config_file)
        keys = [config.lookup(host).get('identityfile', None)
            for host in env.hosts]
        env.key_filename = [expanduser(key) for key in keys if key is not None]
        env.hosts = [hostinfo(host, config) for host in env.hosts]

annotate_hosts_with_ssh_config_info()

@hosts('my_site')
def my_func():
    run('uptime') # The trouble is this will now fail

######## End of Extract

So I try this from the cli and it fails (as expected): 

$ fab my_func
[my_site] Executing task 'my_func'
[my_site] run: uptime

Fatal error: Name lookup failed for sites1

Aborting.

I'm not sure what the best solution is in the short run. Perhaps just hardcode the ugly details and live with the difficulties?

Thanks for reading this far.

Regards,
Carlton

reply via email to

[Prev in Thread] Current Thread [Next in Thread]