Connecting to the node specified in ~ / .ssh / config when using Fabric

I am having problems with Fabric without recognizing the hosts that I have in ~/.ssh/config .

My fabfile.py looks like this:

 from fabric.api import run, env env.hosts = ['lulu'] def whoami(): run('whoami') 

Running $ fab whoami gives:

[lulu] run: whoami

Fatal error: name search failed for Lula

The name lulu is in my ~/.ssh/config , for example:

 Host lulu hostname 192.168.100.100 port 2100 IdentityFile ~/.ssh/lulu-key 

My first thought is to add something like lulu.lulu to /etc/hosts (I am on a Mac), but then I also have to pass the Fabric identification file - and I would rather keep my authentication (i.e. ~/.ssh/config ) separately from my deployment (i.e. fabfile.py ).

Also, by the way, if you try to connect to the host in the hosts file, fabric.contrib.projects.rsync_project does not seem to recognize the "ports" in hosts.env (ie if you use hosts.env = [lulu:2100] rsync_project call rsync_project be trying to connect to lulu:21 ).

Is there a reason Fabric doesn't recognize this name lulu ?

+78
python ssh fabric
Jun 19 '10 at 21:21
source share
4 answers

Starting with version 1.4.0, Fabric uses your ssh configuration (partially). However, you need to explicitly enable it using

 env.use_ssh_config = True 

somewhere near the top of your file. When you do this, Fabric should read your ssh configuration (from ~/.ssh/config by default or from env.ssh_config_path ).

One warning: if you are using a version older than 1.5.4, it will abort if env.use_ssh_config set, but there is no configuration file. In this case, you can use a workaround, for example:

 if env.ssh_config_path and os.path.isfile(os.path.expanduser(env.ssh_config_path)): env.use_ssh_config = True 
+132
Mar 13 2018-12-12T00:
source share

Note that this also happens when the name is not in /etc/hosts . I had the same problem and I had to add the hostname for this file and ~/.ssh/config .

+9
Apr 15 '11 at 8:30
source share

update : this answer is now deprecated .




There is currently no support for the .ssh / config Fabric file. You can set them in functions to then call cli, for example: production task fab; where the product sets the username, hostname, port and ssh identifier.

As for the rsync project, it should now have the ability to configure the port, and if not, you can always run local ("rsync ..."), since that is essentially what this function does.

+5
02 Sep 2018-10-14T00:
source share

You can use the following code to read the configuration (source code taken from: http://markpasc.typepad.com/blog/2010/04/loading-ssh-config-settings-for-fabric.html ):

 from fabric.api import * env.hosts = ['servername'] def _annotate_hosts_with_ssh_config_info(): from os.path import expanduser from paramiko.config import SSHConfig def hostinfo(host, config): hive = config.lookup(host) if 'hostname' in hive: host = hive['hostname'] if 'user' in hive: host = '%s@%s' % (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] for role, rolehosts in env.roledefs.items(): env.roledefs[role] = [hostinfo(host, config) for host in rolehosts] _annotate_hosts_with_ssh_config_info() 
+4
Sep 20 '11 at 11:07
source share



All Articles