From 645b7a2dff1a13d993645e874a7571cf8d2ced84 Mon Sep 17 00:00:00 2001 From: cocoy Date: Mon, 23 Apr 2012 09:48:42 +0800 Subject: [PATCH] Add .ssh/config support --- lib/ansible/connection.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) mode change 100755 => 100644 lib/ansible/connection.py diff --git a/lib/ansible/connection.py b/lib/ansible/connection.py old mode 100755 new mode 100644 index 1af307a232..786463996b --- a/lib/ansible/connection.py +++ b/lib/ansible/connection.py @@ -36,6 +36,8 @@ from ansible import errors ################################################ + + class Connection(object): ''' Handles abstract connections to remote hosts ''' @@ -73,16 +75,40 @@ class ParamikoConnection(object): self.port = self.runner.remote_port def _get_conn(self): + credentials = None + user = self.runner.remote_user + keypair = None + + # Read file ~/.ssh/config, get data hostname, keyfile, port, etc + # This overrides the ansible defined username,hostname and port + try: + ssh_config = paramiko.SSHConfig() + config_file = ('~/.ssh/config') + ssh_config.parse(open(os.path.expanduser(config_file))) + credentials = ssh_config.lookup(self.host) + if 'hostname' in credentials: + self.host = credentials['hostname'] + if 'port' in credentials: + self.port = credentials['port'] + except IOError,e: + raise errors.AnsibleConnectionFailed(str(e)) + + if 'user' in credentials: + user = credentials['user'] + if 'identityfile' in credentials: + keypair = credentials['identityfile'] + ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ssh.connect( self.host, - username=self.runner.remote_user, + username=user, allow_agent=True, look_for_keys=True, password=self.runner.remote_pass, + key_filename=keypair, timeout=self.runner.timeout, port=self.port )