diff --git a/hacking/test-module b/hacking/test-module index 231b0555aed..cdf9dfbf337 100755 --- a/hacking/test-module +++ b/hacking/test-module @@ -83,7 +83,7 @@ except: print "***********************************" print "PARSED OUTPUT" -print utils.bigjson(results) +print utils.jsonify(results,format=True) sys.exit(0) diff --git a/lib/ansible/callbacks.py b/lib/ansible/callbacks.py index 4e092a0b4dd..3bd5d9077fe 100644 --- a/lib/ansible/callbacks.py +++ b/lib/ansible/callbacks.py @@ -76,6 +76,14 @@ class AggregateStats(object): ######################################################################## +def regular_generic_msg(hostname, result, oneline, caption): + ''' output on the result of a module run that is not command ''' + if not oneline: + return "%s | %s >> %s\n" % (hostname, caption, utils.jsonify(result,format=True)) + else: + return "%s | %s >> %s\n" % (hostname, caption, utils.jsonify(result)) + + def banner(msg): res = "" global COWSAY @@ -94,7 +102,45 @@ def banner(msg): else: res = "\n%s ********************* " % msg return res - + +def command_generic_msg(hostname, result, oneline, caption): + ''' output the result of a command run ''' + rc = result.get('rc', '0') + stdout = result.get('stdout','') + stderr = result.get('stderr', '') + msg = result.get('msg', '') + if not oneline: + buf = "%s | %s | rc=%s >>\n" % (hostname, caption, result.get('rc',0)) + if stdout: + buf += stdout + if stderr: + buf += stderr + if msg: + buf += msg + buf += "\n" + return buf + else: + if stderr: + return "%s | %s | rc=%s | (stdout) %s (stderr) %s\n" % (hostname, caption, rc, stdout, stderr) + else: + return "%s | %s | rc=%s | (stdout) %s\n" % (hostname, caption, rc, stdout) + +def host_report_msg(hostname, module_name, result, oneline): + ''' summarize the JSON results for a particular host ''' + failed = utils.is_failed(result) + if module_name in [ 'command', 'shell', 'raw' ] and 'ansible_job_id' not in result: + if not failed: + return command_generic_msg(hostname, result, oneline, 'success') + else: + return command_generic_msg(hostname, result, oneline, 'FAILED') + else: + if not failed: + return regular_generic_msg(hostname, result, oneline, 'success') + else: + return regular_generic_msg(hostname, result, oneline, 'FAILED') + + +############################################### class DefaultRunnerCallbacks(object): ''' no-op callbacks for API usage of Runner() if no callbacks are specified ''' @@ -150,7 +196,10 @@ class CliRunnerCallbacks(DefaultRunnerCallbacks): res = res.get('msg','') print "%s | FAILED => %s" % (host, res) if self.options.tree: - utils.write_tree_file(self.options.tree, host, utils.bigjson(dict(failed=True, msg=res))) + utils.write_tree_file( + self.options.tree, host, + utils.jsonify(dict(failed=True, msg=res),format=True) + ) def on_skipped(self, host): pass @@ -169,15 +218,15 @@ class CliRunnerCallbacks(DefaultRunnerCallbacks): print " polling, %ss remaining"%(jid, clock) def on_async_ok(self, host, res, jid): - print " finished on %s => %s"%(jid, host, utils.bigjson(res)) + print " finished on %s => %s"%(jid, host, utils.jsonify(res,format=True)) def on_async_failed(self, host, res, jid): - print " FAILED on %s => %s"%(jid, host, utils.bigjson(res)) + print " FAILED on %s => %s"%(jid, host, utils.jsonify(res,format=True)) def _on_any(self, host, result): - print utils.host_report_msg(host, self.options.module_name, result, self.options.one_line) + print host_report_msg(host, self.options.module_name, result, self.options.one_line) if self.options.tree: - utils.write_tree_file(self.options.tree, host, utils.bigjson(result)) + utils.write_tree_file(self.options.tree, host, utils.json(result,format=True)) ######################################################################## @@ -193,14 +242,14 @@ class PlaybookRunnerCallbacks(DefaultRunnerCallbacks): print "fatal: [%s] => %s" % (host, msg) def on_failed(self, host, results): - print "failed: [%s] => %s\n" % (host, utils.smjson(results)) + print "failed: [%s] => %s\n" % (host, utils.jsonify(results)) def on_ok(self, host, host_result): # show verbose output for non-setup module results if --verbose is used if not self.verbose or host_result.get("verbose_override",None) is not None: print "ok: [%s]" % (host) else: - print "ok: [%s] => %s" % (host, utils.smjson(host_result)) + print "ok: [%s] => %s" % (host, utils.jsonify(host_result)) def on_error(self, host, err): print >>sys.stderr, "err: [%s] => %s\n" % (host, err) diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 0ecdfefbbe8..649c403e78a 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -220,7 +220,7 @@ class Runner(object): ''' transfer string to remote file ''' if type(data) == dict: - data = utils.smjson(data) + data = utils.jsonify(data) afd, afile = tempfile.mkstemp() afo = os.fdopen(afd, 'w') @@ -326,7 +326,7 @@ class Runner(object): args = self._add_setup_vars(inject, args) if type(args) == dict: - args = utils.bigjson(args) + args = utils.jsonify(args,format=True) args = utils.template(args, inject, self.setup_cache) @@ -650,7 +650,7 @@ class Runner(object): conditional = utils.template(self.conditional, inject, self.setup_cache) if not eval(conditional): - result = utils.smjson(dict(skipped=True)) + result = utils.jsonify(dict(skipped=True)) self.callbacks.on_skipped(host) return ReturnData(host=host, result=result) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index 1a5c7f3b55c..4c85ff29aad 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -39,7 +39,6 @@ try: except ImportError: from md5 import md5 as _md5 - ############################################################### # UTILITY FUNCTIONS FOR COMMAND LINE TOOLS ############################################################### @@ -53,60 +52,13 @@ def exit(msg, rc=1): err(msg) sys.exit(rc) -def bigjson(result): - ''' format JSON output (uncompressed) ''' - result2 = result.copy() - return json.dumps(result2, sort_keys=True, indent=4) - -def smjson(result): - ''' format JSON output (compressed) ''' +def jsonify(result, format=False): + ''' format JSON output (uncompressed or uncompressed) ''' result2 = result.copy() - return json.dumps(result2, sort_keys=True) - -def regular_generic_msg(hostname, result, oneline, caption): - ''' output on the result of a module run that is not command ''' - if not oneline: - return "%s | %s >> %s\n" % (hostname, caption, bigjson(result)) - else: - return "%s | %s >> %s\n" % (hostname, caption, smjson(result)) - -def regular_success_msg(hostname, result, oneline): - ''' output the result of a successful module run ''' - return regular_generic_msg(hostname, result, oneline, 'success') - -def regular_failure_msg(hostname, result, oneline): - ''' output the result of a failed module run ''' - return regular_generic_msg(hostname, result, oneline, 'FAILED') - -def command_generic_msg(hostname, result, oneline, caption): - ''' output the result of a command run ''' - rc = result.get('rc', '0') - stdout = result.get('stdout','') - stderr = result.get('stderr', '') - msg = result.get('msg', '') - if not oneline: - buf = "%s | %s | rc=%s >>\n" % (hostname, caption, result.get('rc',0)) - if stdout: - buf += stdout - if stderr: - buf += stderr - if msg: - buf += msg - buf += "\n" - return buf + if format: + return json.dumps(result2, sort_keys=True, indent=4) else: - if stderr: - return "%s | %s | rc=%s | (stdout) %s (stderr) %s\n" % (hostname, caption, rc, stdout, stderr) - else: - return "%s | %s | rc=%s | (stdout) %s\n" % (hostname, caption, rc, stdout) - -def command_success_msg(hostname, result, oneline): - ''' output from a successful command run ''' - return command_generic_msg(hostname, result, oneline, 'success') - -def command_failure_msg(hostname, result, oneline): - ''' output from a failed command run ''' - return command_generic_msg(hostname, result, oneline, 'FAILED') + return json.dumps(result2, sort_keys=True) def write_tree_file(tree, hostname, buf): ''' write something into treedir/hostname ''' @@ -128,22 +80,6 @@ def is_failed(result): return True return failed -def host_report_msg(hostname, module_name, result, oneline): - ''' summarize the JSON results for a particular host ''' - buf = '' - failed = is_failed(result) - if module_name in [ 'command', 'shell', 'raw' ] and 'ansible_job_id' not in result: - if not failed: - buf = command_success_msg(hostname, result, oneline) - else: - buf = command_failure_msg(hostname, result, oneline) - else: - if not failed: - buf = regular_success_msg(hostname, result, oneline) - else: - buf = regular_failure_msg(hostname, result, oneline) - return buf - def prepare_writeable_dir(tree): ''' make sure a directory exists and is writeable ''' if tree != '/': diff --git a/test/TestPlayBook.py b/test/TestPlayBook.py index fed39a27493..07e0f71c302 100644 --- a/test/TestPlayBook.py +++ b/test/TestPlayBook.py @@ -138,7 +138,6 @@ class TestPlaybook(unittest.TestCase): runner_callbacks = self.test_callbacks ) result = self.playbook.run() - # print utils.bigjson(dict(events=EVENTS)) return result def test_one(self): @@ -147,7 +146,7 @@ class TestPlaybook(unittest.TestCase): # if different, this will output to screen print "**ACTUAL**" - print utils.bigjson(actual) + print utils.jsonify(actual, format=True) expected = { "127.0.0.2": { "changed": 9, @@ -158,8 +157,9 @@ class TestPlaybook(unittest.TestCase): } } print "**EXPECTED**" - print utils.bigjson(expected) - assert utils.bigjson(expected) == utils.bigjson(actual) + print utils.jsonify(expected, format=True) + + assert utils.jsonify(expected, format=True) == utils.jsonify(actual,format=True) # make sure the template module took options from the vars section data = file('/tmp/ansible_test_data_template.out').read()