From 72715c6adde78e9d497e3f535eae7a97e8f34006 Mon Sep 17 00:00:00 2001 From: Abhijit Menon-Sen Date: Fri, 31 Jul 2015 18:57:20 +0530 Subject: [PATCH] Fix ssh connections to IPv6 addresses We can unconditionally wrap remote_addr in square brackets for scp and sftp (both of which require them for IPv6 addresses), but not wrap them at all for ssh (which doesn't accept them). This way, we don't have to detect and treat IPv6 addresses specially. This works for hostnames, IPv4 addresses, and IPv6 addresses. The earlier code seemed to intend to wrap all IPv6 addresses in square brackets, which would have broken ssh, but it actually made no attempt to detect IPv6 addresses at all (so it broke only with IPv6 addresses for scp and sftp). Based on a review of PR #11677 by @JuiceBoxSingularity --- lib/ansible/plugins/connections/ssh.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/ansible/plugins/connections/ssh.py b/lib/ansible/plugins/connections/ssh.py index 66ba14d5bbc..5231c8ae8c0 100644 --- a/lib/ansible/plugins/connections/ssh.py +++ b/lib/ansible/plugins/connections/ssh.py @@ -57,11 +57,7 @@ class Connection(ConnectionBase): super(Connection, self).__init__(*args, **kwargs) - # FIXME: make this work, should be set from connection info - self._ipv6 = False self.host = self._play_context.remote_addr - if self._ipv6: - self.host = '[%s]' % self.host @property def transport(self): @@ -342,8 +338,6 @@ class Connection(ConnectionBase): ssh_cmd.append("-q") ssh_cmd += self._common_args - if self._ipv6: - ssh_cmd += ['-6'] ssh_cmd.append(self.host) ssh_cmd.append(cmd) @@ -437,15 +431,19 @@ class Connection(ConnectionBase): raise AnsibleFileNotFound("file or module does not exist: {0}".format(in_path)) cmd = self._password_cmd() + # scp and sftp require square brackets for IPv6 addresses, but + # accept them for hostnames and IPv4 addresses too. + host = '[%s]' % self.host + if C.DEFAULT_SCP_IF_SSH: cmd.append('scp') cmd.extend(self._common_args) - cmd.extend([in_path, '{0}:{1}'.format(self.host, pipes.quote(out_path))]) + cmd.extend([in_path, '{0}:{1}'.format(host, pipes.quote(out_path))]) indata = None else: cmd.append('sftp') cmd.extend(self._common_args) - cmd.append(self.host) + cmd.append(host) indata = "put {0} {1}\n".format(pipes.quote(in_path), pipes.quote(out_path)) (p, stdin) = self._run(cmd, indata)