1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
This commit is contained in:
Michael DeHaan 2012-08-11 11:48:57 -04:00
commit 3cc564c127
3 changed files with 35 additions and 11 deletions

View file

@ -655,7 +655,7 @@ class Runner(object):
cmd = " || ".join(md5s)
cmd = "%s; %s || (echo \"${rc} %s\")" % (test, cmd, path)
return self._low_level_exec_command(conn, cmd, tmp, sudoable=False).split()[0]
return utils.last_non_blank_line(self._low_level_exec_command(conn, cmd, tmp, sudoable=False))
# *****************************************************
@ -675,7 +675,7 @@ class Runner(object):
cmd += ' && echo %s' % basetmp
result = self._low_level_exec_command(conn, cmd, None, sudoable=False)
return result.split("\n")[0].strip() + '/'
return utils.last_non_blank_line(result).strip() + '/'
# *****************************************************

View file

@ -98,16 +98,12 @@ class SSHConnection(object):
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# We can't use p.communicate here because the ControlMaster may have stdout open as well
p.stdin.close()
stdout = ''
while p.poll() is None:
rfd, wfd, efd = select.select([p.stdout], [], [p.stdout], 1)
if p.stdout in rfd:
stdout += os.read(p.stdout.fileno(), 1024)
# older versions of ssh generate this error which we ignore
stdout=stdout.replace("tcgetattr: Invalid argument\n", "")
# suppress Ubuntu 10.04/12.04 error on -tt option
stdout=stdout.replace("tcgetattr: Inappropriate ioctl for device\n","")
p.stdin.close() # close stdin after we read from stdout (see also issue #848)
if p.returncode != 0 and stdout.find('Bad configuration option: ControlPersist') != -1:
raise errors.AnsibleError('using -c ssh on certain older ssh versions may not support ControlPersist, set ANSIBLE_SSH_ARGS="" before running again')

View file

@ -117,9 +117,11 @@ def json_loads(data):
return json.loads(data)
def parse_json(data):
def parse_json(raw_data):
''' this version for module return data only '''
data = filter_leading_non_json_lines(raw_data)
try:
return json.loads(data)
except:
@ -415,3 +417,29 @@ def do_encrypt(result, encrypt, salt_size=None, salt=None):
return result
def last_non_blank_line(lines):
all_lines = lines.splitlines()
all_lines.reverse()
for line in all_lines:
if (len(line) > 0):
return line
return "" # we shouldn't come here (no lines?) but let's pretend nothing happend
# We can't return all lines here because calling code expects only one
# line. And since we don't know which line to return we return an empty
# line.
def is_valid_json_line(line):
return line.startswith('=') or line.startswith('{') or line.startswith('[')
def filter_leading_non_json_lines(lines):
''' we need to filter anything which starts not with '{', '[', ', '=' or is an empty line.
But we filter only leading lines since multiline JSON is valid. '''
filtered_lines = ''
no_more_filtering = False
for line in lines.splitlines():
if (no_more_filtering or is_valid_json_line(line)):
no_more_filtering = True
filtered_lines += line + '\n'
return filtered_lines