From 37ae5aab31ad10bf4e194b54e09050d5dbd807ef Mon Sep 17 00:00:00 2001 From: alberto Date: Thu, 28 May 2015 12:19:32 +0200 Subject: [PATCH] Capture only IOError when reading shebang from inventory file, to avoid ignoring other possible exceptions like timeouts from a task --- lib/ansible/inventory/__init__.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index f012246e22..e4080e39d8 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -105,19 +105,18 @@ class Inventory(object): # class we can show a more apropos error shebang_present = False try: - inv_file = open(host_list) - first_line = inv_file.readlines()[0] - inv_file.close() - if first_line.startswith('#!'): - shebang_present = True - except: + with open(host_list, "r") as inv_file: + first_line = inv_file.readline() + if first_line.startswith("#!"): + shebang_present = True + except IOError: pass if utils.is_executable(host_list): try: self.parser = InventoryScript(filename=host_list) self.groups = self.parser.groups.values() - except: + except errors.AnsibleError: if not shebang_present: raise errors.AnsibleError("The file %s is marked as executable, but failed to execute correctly. " % host_list + \ "If this is not supposed to be an executable script, correct this with `chmod -x %s`." % host_list) @@ -127,7 +126,7 @@ class Inventory(object): try: self.parser = InventoryParser(filename=host_list) self.groups = self.parser.groups.values() - except: + except errors.AnsibleError: if shebang_present: raise errors.AnsibleError("The file %s looks like it should be an executable inventory script, but is not marked executable. " % host_list + \ "Perhaps you want to correct this with `chmod +x %s`?" % host_list)