From 9c5ec886a6c053b0ca421ce0b79fd2f5d685c254 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sun, 18 Mar 2012 17:53:58 -0400 Subject: [PATCH] Import cleanup, plus have /bin/ansible remind you if no hosts were matched by a pattern (for instance, assume there was a typo in the pattern) --- Makefile | 2 +- bin/ansible | 3 +++ lib/ansible/runner.py | 13 ++++--------- lib/ansible/utils.py | 12 ++++++++++++ 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 45997033b44..3e37a55e679 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 0d79b1ddf09..480112abd4e 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 aabcbea2326..02705f3ebc5 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 8acee184d8a..993762e6e75 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: