diff --git a/Makefile b/Makefile index 45997033b4..3e37a55e67 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ pep8: pep8 -r --ignore=E501,E221,W291,W391,E302,E251,E203,W293,E231,E303,E201,E225 lib/ bin/ pyflakes: - pyflakes lib/ansible/*.py + pyflakes lib/ansible/*.py bin/* clean: @echo "Cleaning up distutils stuff" diff --git a/bin/ansible b/bin/ansible index 0d79b1ddf0..480112abd4 100755 --- a/bin/ansible +++ b/bin/ansible @@ -181,6 +181,9 @@ class Cli(object): if utils.has_dark_hosts(results): buf += utils.dark_hosts_msg(results) + if not utils.has_hosts(results): + print "ERROR: no hosts matched" + print buf ######################################################## diff --git a/lib/ansible/runner.py b/lib/ansible/runner.py index aabcbea232..02705f3ebc 100755 --- a/lib/ansible/runner.py +++ b/lib/ansible/runner.py @@ -22,8 +22,6 @@ import fnmatch import multiprocessing import signal import os -import ansible.constants as C -import ansible.connection import Queue import random import jinja2 @@ -31,17 +29,14 @@ import traceback import tempfile import subprocess +import ansible.constants as C +import ansible.connection from ansible import utils from ansible import errors # should be True except in debug CLEANUP_FILES = True -try: - import json -except ImportError: - import simplejson as json - ################################################ def _executor_hook(job_queue, result_queue): @@ -170,7 +165,7 @@ class Runner(object): cmd = subprocess.Popen([host_list], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False) out, err = cmd.communicate() try: - groups = json.loads(out) + groups = utils.json_loads(out) except: raise errors.AnsibleError("invalid JSON response from script: %s" % host_list) for (groupname, hostlist) in groups.iteritems(): @@ -309,7 +304,7 @@ class Runner(object): out, err = cmd.communicate() inject2 = {} try: - inject2 = json.loads(out) + inject2 = utils.json_loads(out) except: raise errors.AnsibleError("%s returned invalid result when called with hostname %s" % ( Runner._external_variable_script, diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index 8acee184d8..993762e6e7 100755 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -149,6 +149,14 @@ def has_dark_hosts(results): ''' are there any uncontactable hosts? ''' return len(results['dark'].keys()) > 0 +def has_contacted_hosts(results): + ''' are there any contacted hosts? ''' + return len(results['contacted'].keys()) > 0 + +def has_hosts(results): + ''' did ansible run against any hosts at all? ''' + return has_contacted_hosts(results) or has_dark_hosts(results) + def contacted_hosts(results): ''' what are the contactable hosts? ''' return sorted(results['contacted']) @@ -186,7 +194,11 @@ def async_poll_status(jid, host, clock, result): else: return " polling on %s, %s remaining" % (jid, host, clock) +def json_loads(data): + return json.loads(data) + def parse_json(data): + ''' this version for module return data only ''' try: return json.loads(data) except: