From 1cf43e9afe5ee82da0d9ebfc56c3a9e3feece870 Mon Sep 17 00:00:00 2001
From: "martin f. krafft" <madduck@madduck.net>
Date: Fri, 15 Feb 2013 10:15:51 +1300
Subject: [PATCH 1/2] Only allocate a PTY when sudo is used

Postpone the paramiko.Channel.get_pty until we know sudo is used. If
sudo is not used, then we do not need a PTY. In fact, the paramiko docs
explicitly state that it's not desirable to allocate a PTY for a simple
exec_command.

Signed-off-by: martin f. krafft <madduck@madduck.net>
---
 lib/ansible/runner/connection_plugins/paramiko_ssh.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/ansible/runner/connection_plugins/paramiko_ssh.py b/lib/ansible/runner/connection_plugins/paramiko_ssh.py
index 67b5ce83cd7..f3e293c8fe9 100644
--- a/lib/ansible/runner/connection_plugins/paramiko_ssh.py
+++ b/lib/ansible/runner/connection_plugins/paramiko_ssh.py
@@ -110,7 +110,6 @@ class Connection(object):
             if len(str(e)) > 0:
                 msg += ": %s" % str(e)
             raise errors.AnsibleConnectionFailed(msg)
-        chan.get_pty()
 
         if not self.runner.sudo or not sudoable:
             if executable:
@@ -120,6 +119,7 @@ class Connection(object):
             vvv("EXEC %s" % quoted_command, host=self.host)
             chan.exec_command(quoted_command)
         else:
+            chan.get_pty()
             shcmd, prompt = utils.make_sudo_cmd(sudo_user, executable, cmd)
             vvv("EXEC %s" % shcmd, host=self.host)
             sudo_output = ''

From ab3990926059947e1a90503ba0ed643b5cf395a0 Mon Sep 17 00:00:00 2001
From: "martin f. krafft" <madduck@madduck.net>
Date: Fri, 15 Feb 2013 10:23:34 +1300
Subject: [PATCH 2/2] Initialise PTY from calling environment

If we need to acquire a PTY for sudo's use, then it should really
inherit the capabilities of the calling environment. This is what
OpenSSH does, and so it makes sense to copy this behaviour for the
paramiko connection type.

Closes: #2065
Signed-off-by: martin f. krafft <madduck@madduck.net>
---
 lib/ansible/runner/connection_plugins/paramiko_ssh.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lib/ansible/runner/connection_plugins/paramiko_ssh.py b/lib/ansible/runner/connection_plugins/paramiko_ssh.py
index f3e293c8fe9..6700efa8c73 100644
--- a/lib/ansible/runner/connection_plugins/paramiko_ssh.py
+++ b/lib/ansible/runner/connection_plugins/paramiko_ssh.py
@@ -119,7 +119,12 @@ class Connection(object):
             vvv("EXEC %s" % quoted_command, host=self.host)
             chan.exec_command(quoted_command)
         else:
-            chan.get_pty()
+            # sudo usually requires a PTY (cf. requiretty option), therefore
+            # we give it one, and we try to initialise from the calling
+            # environment
+            chan.get_pty(term=os.getenv('TERM', 'vt100'),
+                         width=os.getenv('COLUMNS', 0),
+                         height=os.getenv('LINES', 0))
             shcmd, prompt = utils.make_sudo_cmd(sudo_user, executable, cmd)
             vvv("EXEC %s" % shcmd, host=self.host)
             sudo_output = ''