From b5faf88494e61cf78e336fe35d4767706abd3583 Mon Sep 17 00:00:00 2001 From: Steve Gargan Date: Mon, 16 Mar 2015 16:50:53 +0000 Subject: [PATCH] Properly report exception causes particularly connection exceptions contacting the consul agent --- lib/ansible/modules/extras/clustering/consul | 11 ++++++----- lib/ansible/modules/extras/clustering/consul_acl | 11 ++++++----- lib/ansible/modules/extras/clustering/consul_kv | 12 +++++++----- lib/ansible/modules/extras/clustering/consul_session | 11 ++++++----- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/ansible/modules/extras/clustering/consul b/lib/ansible/modules/extras/clustering/consul index 24df908c45..15a68f068a 100644 --- a/lib/ansible/modules/extras/clustering/consul +++ b/lib/ansible/modules/extras/clustering/consul @@ -143,6 +143,7 @@ except ImportError, e: "see http://python-consul.readthedocs.org/en/latest/#installation'" sys.exit(1) +from requests.exceptions import ConnectionError def register_with_consul(module): @@ -453,11 +454,11 @@ def main(): ) try: register_with_consul(module) - except IOError, e: - error = e.read() - if not error: - error = str(e) - module.fail_json(msg=error) + except ConnectionError, e: + module.fail_json(msg='Could not connect to consul agent at %s:%s, error was %s' % ( + module.params.get('host'), module.params.get('port'), str(e))) + except Exception, e: + module.fail_json(msg=str(e)) # import module snippets from ansible.module_utils.basic import * diff --git a/lib/ansible/modules/extras/clustering/consul_acl b/lib/ansible/modules/extras/clustering/consul_acl index fc997400ae..cd5466c53b 100644 --- a/lib/ansible/modules/extras/clustering/consul_acl +++ b/lib/ansible/modules/extras/clustering/consul_acl @@ -92,6 +92,7 @@ except ImportError: " see https://pypi.python.org/pypi/pyhcl'" sys.exit(1) +from requests.exceptions import ConnectionError def execute(module): @@ -284,11 +285,11 @@ def main(): try: execute(module) - except IOError, e: - error = e.read() - if not error: - error = str(e) - module.fail_json(msg=error) + except ConnectionError, e: + module.fail_json(msg='Could not connect to consul agent at %s:%s, error was %s' % ( + module.params.get('host'), module.params.get('port'), str(e))) + except Exception, e: + module.fail_json(msg=str(e)) # import module snippets from ansible.module_utils.basic import * diff --git a/lib/ansible/modules/extras/clustering/consul_kv b/lib/ansible/modules/extras/clustering/consul_kv index 6a2b77ea7c..8999a43319 100644 --- a/lib/ansible/modules/extras/clustering/consul_kv +++ b/lib/ansible/modules/extras/clustering/consul_kv @@ -117,6 +117,7 @@ except ImportError, e: see http://python-consul.readthedocs.org/en/latest/#installation'""" sys.exit(1) +from requests.exceptions import ConnectionError def execute(module): @@ -227,11 +228,12 @@ def main(): try: execute(module) - except IOError, e: - error = e.read() - if not error: - error = str(e) - module.fail_json(msg=error) + except ConnectionError, e: + module.fail_json(msg='Could not connect to consul agent at %s:%s, error was %s' % ( + module.params.get('host'), module.params.get('port'), str(e))) + except Exception, e: + module.fail_json(msg=str(e)) + # import module snippets from ansible.module_utils.basic import * diff --git a/lib/ansible/modules/extras/clustering/consul_session b/lib/ansible/modules/extras/clustering/consul_session index f11c5447e5..00f4cae734 100644 --- a/lib/ansible/modules/extras/clustering/consul_session +++ b/lib/ansible/modules/extras/clustering/consul_session @@ -80,6 +80,7 @@ except ImportError, e: "http://python-consul.readthedocs.org/en/latest/#installation'" sys.exit(1) +from requests.errors import ConnectionError def execute(module): @@ -202,11 +203,11 @@ def main(): try: execute(module) - except IOError, e: - error = e.read() - if not error: - error = str(e) - module.fail_json(msg=error) + except ConnectionError, e: + module.fail_json(msg='Could not connect to consul agent at %s:%s, error was %s' % ( + module.params.get('host'), module.params.get('port'), str(e))) + except Exception, e: + module.fail_json(msg=str(e)) # import module snippets from ansible.module_utils.basic import *