Add .ssh/config support

This commit is contained in:
cocoy 2012-04-23 09:48:42 +08:00
parent ec56b30248
commit 645b7a2dff

28
lib/ansible/connection.py Executable file → Normal file
View file

@ -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
)