diff --git a/lib/ansible/runner/connection.py b/lib/ansible/runner/connection.py index e0bada9eadb..bb50bf5531f 100644 --- a/lib/ansible/runner/connection.py +++ b/lib/ansible/runner/connection.py @@ -20,6 +20,7 @@ import os import stat +import errno from ansible import utils from ansible.errors import AnsibleError @@ -36,15 +37,14 @@ class Connector(object): raise AnsibleError("unsupported connection type: %s" % transport) if private_key_file: # If private key is readable by user other than owner, flag an error + st = None try: st = os.stat(private_key_file) - except IOError, e: - if e.errno == errno.ENOENT: # file is missing, might be agent - st = { 'st_mode': False } - else: + except (IOError, OSError), e: + if e.errno != errno.ENOENT: # file is missing, might be agent raise(e) - if st.st_mode & (stat.S_IRGRP | stat.S_IROTH): + if st is not None and st.st_mode & (stat.S_IRGRP | stat.S_IROTH): raise AnsibleError("private_key_file (%s) is group-readable or world-readable and thus insecure - " "you will probably get an SSH failure" % (private_key_file,))