mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Because paramiko using a pty can't distinguish stderr and stdout, remove handling that
treated -D as a way to show stderr, and make sure modules don't include things on stderr. Update docs and test module script to come into line.
This commit is contained in:
parent
076f1bc169
commit
36e454c52f
10 changed files with 47 additions and 54 deletions
|
@ -122,7 +122,7 @@ class CliRunnerCallbacks(DefaultRunnerCallbacks):
|
|||
pass
|
||||
|
||||
def on_error(self, host, err):
|
||||
print >>sys.stderr, "stderr: [%s] => %s\n" % (host, err)
|
||||
print >>sys.stderr, "err: [%s] => %s\n" % (host, err)
|
||||
|
||||
def on_no_hosts(self):
|
||||
print >>sys.stderr, "no hosts matched\n"
|
||||
|
@ -141,7 +141,7 @@ class PlaybookRunnerCallbacks(DefaultRunnerCallbacks):
|
|||
self.stats = stats
|
||||
|
||||
def on_unreachable(self, host, msg):
|
||||
print "unreachable: [%s] => %s" % (host, msg)
|
||||
print "fatal: [%s] => %s" % (host, msg)
|
||||
|
||||
def on_failed(self, host, results):
|
||||
invocation = results.get('invocation',None)
|
||||
|
@ -160,7 +160,7 @@ class PlaybookRunnerCallbacks(DefaultRunnerCallbacks):
|
|||
print "ok: [%s] => %s\n" % (host, invocation)
|
||||
|
||||
def on_error(self, host, err):
|
||||
print >>sys.stderr, "stderr: [%s] => %s\n" % (host, err)
|
||||
print >>sys.stderr, "err: [%s] => %s\n" % (host, err)
|
||||
|
||||
def on_skipped(self, host):
|
||||
print "skipping: [%s]\n" % host
|
||||
|
|
|
@ -39,9 +39,6 @@ with warnings.catch_warnings():
|
|||
|
||||
################################################
|
||||
|
||||
RANDOM_PROMPT_LEN = 32 # 32 random chars in [a-z] gives > 128 bits of entropy
|
||||
|
||||
|
||||
class Connection(object):
|
||||
''' Handles abstract connections to remote hosts '''
|
||||
|
||||
|
@ -96,12 +93,8 @@ class ParamikoConnection(object):
|
|||
except IOError,e:
|
||||
raise errors.AnsibleConnectionFailed(str(e))
|
||||
|
||||
#if 'hostname' in credentials:
|
||||
# self.host = credentials['hostname']
|
||||
if 'port' in credentials:
|
||||
self.port = int(credentials['port'])
|
||||
#if 'user' in credentials:
|
||||
# user = credentials['user']
|
||||
if 'identityfile' in credentials:
|
||||
keypair = os.path.expanduser(credentials['identityfile'])
|
||||
|
||||
|
@ -133,14 +126,22 @@ class ParamikoConnection(object):
|
|||
self.ssh = self._get_conn()
|
||||
return self
|
||||
|
||||
def exec_command(self, cmd, tmp_path, sudoable=False): # pylint: disable-msg=W0613
|
||||
def exec_command(self, cmd, tmp_path, sudoable=False):
|
||||
|
||||
''' run a command on the remote host '''
|
||||
bufsize = 4096 # Could make this a Runner param if needed
|
||||
timeout_secs = self.runner.timeout # Reusing runner's TCP connect timeout as command progress timeout
|
||||
|
||||
bufsize = 4096
|
||||
# reusing runner's TCP connect timeout as command progress timeout (correct?)
|
||||
timeout_secs = self.runner.timeout
|
||||
chan = self.ssh.get_transport().open_session()
|
||||
chan.settimeout(timeout_secs)
|
||||
chan.get_pty() # Many sudo setups require a terminal; use in both cases for consistency
|
||||
|
||||
stdin = chan.makefile('wb', bufsize)
|
||||
stdout = chan.makefile('rb', bufsize)
|
||||
stderr = chan.makefile_stderr('rb', bufsize)
|
||||
chan.get_pty()
|
||||
chan.set_combine_stderr(False)
|
||||
|
||||
|
||||
if not self.runner.sudo or not sudoable:
|
||||
quoted_command = '"$SHELL" -c ' + pipes.quote(cmd)
|
||||
chan.exec_command(quoted_command)
|
||||
|
@ -151,17 +152,26 @@ class ParamikoConnection(object):
|
|||
# follows. Passing a quoted compound command to sudo (or sudo -s)
|
||||
# directly doesn't work, so we shellquote it with pipes.quote()
|
||||
# and pass the quoted string to the user's shell.
|
||||
sudocmd = 'sudo -k -- "$SHELL" -c ' + pipes.quote(cmd)
|
||||
chan.exec_command(sudocmd)
|
||||
if self.runner.sudo_pass:
|
||||
while not chan.recv_ready():
|
||||
time.sleep(0.25)
|
||||
sudo_output = chan.recv(bufsize) # Pull prompt, catch errors, eat sudo output
|
||||
chan.sendall(self.runner.sudo_pass + '\n')
|
||||
|
||||
stdin = chan.makefile('wb', bufsize)
|
||||
randbits = ''.join(chr(random.randint(ord('a'), ord('z'))) for x in xrange(32))
|
||||
prompt = '[sudo via ansible, key=%s] password: ' % randbits
|
||||
sudocmd = 'sudo -k -p "%s" -- "$SHELL" -c %s' % (prompt, pipes.quote(cmd))
|
||||
sudo_output = ''
|
||||
try:
|
||||
chan.exec_command(sudocmd)
|
||||
if self.runner.sudo_pass:
|
||||
while not sudo_output.endswith(prompt):
|
||||
chunk = chan.recv(bufsize)
|
||||
if not chunk:
|
||||
raise errors.AnsibleError('ssh connection closed waiting for sudo password prompt')
|
||||
sudo_output += chunk
|
||||
chan.sendall(self.runner.sudo_pass + '\n')
|
||||
except socket.timeout:
|
||||
raise errors.AnsibleError('ssh timed out waiting for sudo.\n' + sudo_output)
|
||||
|
||||
stdin = chan.makefile('wb', bufsize)
|
||||
stdout = chan.makefile('rb', bufsize)
|
||||
stderr = chan.makefile_stderr('rb', bufsize)
|
||||
stderr = chan.makefile_stderr('rb', bufsize)
|
||||
return stdin, stdout, stderr
|
||||
|
||||
def put_file(self, in_path, out_path):
|
||||
|
|
|
@ -637,16 +637,18 @@ class Runner(object):
|
|||
''' execute a command string over SSH, return the output '''
|
||||
|
||||
stdin, stdout, stderr = conn.exec_command(cmd, tmp, sudoable=sudoable)
|
||||
|
||||
err=None
|
||||
out=None
|
||||
if type(stderr) != str:
|
||||
err="\n".join(stderr.readlines())
|
||||
else:
|
||||
err=stderr
|
||||
|
||||
if type(stdout) != str:
|
||||
return "\n".join(stdout.readlines()), err
|
||||
out="\n".join(stdout.readlines())
|
||||
else:
|
||||
return stdout, err
|
||||
out=stdout
|
||||
return (out,err)
|
||||
|
||||
|
||||
# *****************************************************
|
||||
|
||||
|
|
|
@ -286,7 +286,7 @@ def base_parser(constants=C, usage="", output_opts=False, runas_opts=False, asyn
|
|||
|
||||
parser = SortedOptParser(usage)
|
||||
parser.add_option('-D','--debug', default=False, action="store_true",
|
||||
help='debug standard error output of remote modules')
|
||||
help='debug mode')
|
||||
parser.add_option('-f','--forks', dest='forks', default=constants.DEFAULT_FORKS, type='int',
|
||||
help="specify number of parallel processes to use (default=%s)" % constants.DEFAULT_FORKS)
|
||||
parser.add_option('-i', '--inventory-file', dest='inventory',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue