From 3aac18738729897695ff5116d70da21598a572b2 Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Sun, 18 Aug 2013 23:08:35 +0200 Subject: [PATCH] fix ssh connection plugin to work with ipv6 address Due to various inconsistencies of ssh and sftp regarding ipv6 and ipv4 handling, some special arguments must be passed, and the ipv6 must be passed in a specific format. --- lib/ansible/runner/connection_plugins/ssh.py | 22 +++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/ansible/runner/connection_plugins/ssh.py b/lib/ansible/runner/connection_plugins/ssh.py index abbffcf785..ece1021f85 100644 --- a/lib/ansible/runner/connection_plugins/ssh.py +++ b/lib/ansible/runner/connection_plugins/ssh.py @@ -38,6 +38,7 @@ class Connection(object): def __init__(self, runner, host, port, user, password, private_key_file, *args, **kwargs): self.runner = runner self.host = host + self.ipv6 = ':' in self.host self.port = port self.user = user self.password = password @@ -130,7 +131,10 @@ class Connection(object): ''' run a command on the remote host ''' ssh_cmd = self._password_cmd() - ssh_cmd += ["ssh", "-tt", "-q"] + self.common_args + [self.host] + ssh_cmd += ["ssh", "-tt", "-q"] + self.common_args + if self.ipv6: + ssh_cmd += ['-6'] + ssh_cmd += [self.host] if not self.runner.sudo or not sudoable: if executable: @@ -232,12 +236,16 @@ class Connection(object): raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path) cmd = self._password_cmd() + host = self.host + if self.ipv6: + host = '[%s]' % host + if C.DEFAULT_SCP_IF_SSH: cmd += ["scp"] + self.common_args - cmd += [in_path,self.host + ":" + pipes.quote(out_path)] + cmd += [in_path,host + ":" + pipes.quote(out_path)] indata = None else: - cmd += ["sftp"] + self.common_args + [self.host] + cmd += ["sftp"] + self.common_args + [host] indata = "put %s %s\n" % (pipes.quote(in_path), pipes.quote(out_path)) p = subprocess.Popen(cmd, stdin=subprocess.PIPE, @@ -253,12 +261,16 @@ class Connection(object): vvv("FETCH %s TO %s" % (in_path, out_path), host=self.host) cmd = self._password_cmd() + host = self.host + if self.ipv6: + host = '[%s]' % host + if C.DEFAULT_SCP_IF_SSH: cmd += ["scp"] + self.common_args - cmd += [self.host + ":" + in_path, out_path] + cmd += [host + ":" + in_path, out_path] indata = None else: - cmd += ["sftp"] + self.common_args + [self.host] + cmd += ["sftp"] + self.common_args + [host] indata = "get %s %s\n" % (in_path, out_path) p = subprocess.Popen(cmd, stdin=subprocess.PIPE,