diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index a1e566ce8ea..5462b94628c 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -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() + '/' # ***************************************************** diff --git a/lib/ansible/runner/connection/ssh.py b/lib/ansible/runner/connection/ssh.py index 8c84246e4ed..51195aa1f3f 100644 --- a/lib/ansible/runner/connection/ssh.py +++ b/lib/ansible/runner/connection/ssh.py @@ -60,7 +60,7 @@ class SSHConnection(object): ssh_cmd = ["ssh", "-tt", "-q"] + self.common_args + [self.host] if self.runner.sudo and sudoable: # Rather than detect if sudo wants a password this time, -k makes - # sudo always ask for a password if one is required. + # sudo always ask for a password if one is required. # 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. We loop reading @@ -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') diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index e6d9bc80229..a0674731c0e 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -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: @@ -307,10 +309,10 @@ def _gitinfo(): branch = f.readline().split('/')[-1].rstrip("\n") branch_path = os.path.join(repo_path, "refs", "heads", branch) with open(branch_path) as f: - commit = f.readline()[:10] + commit = f.readline()[:10] date = time.localtime(os.stat(branch_path).st_mtime) offset = time.timezone if (time.daylight == 0) else time.altzone - result = "({0} {1}) last updated {2} (GMT {3:+04d})".format(branch, commit, + result = "({0} {1}) last updated {2} (GMT {3:+04d})".format(branch, commit, time.strftime("%Y/%m/%d %H:%M:%S", date), offset / -36) return result @@ -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 +