Remove deprecated get_exception API

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde
2017-12-26 08:33:21 +05:30
committed by Brian Coca
parent caefe31125
commit 6bd0fbb63c
42 changed files with 284 additions and 409 deletions

View File

@@ -81,7 +81,7 @@ import tempfile
import errno
import re
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils._text import to_native
from ansible.module_utils.basic import AnsibleModule
@@ -132,8 +132,7 @@ def enforce_state(module, params):
if replace_or_add or found != (state == "present"):
try:
inf = open(path, "r")
except IOError:
e = get_exception()
except IOError as e:
if e.errno == errno.ENOENT:
inf = None
else:
@@ -150,9 +149,8 @@ def enforce_state(module, params):
outf.write(key)
outf.flush()
module.atomic_move(outf.name, path)
except (IOError, OSError):
e = get_exception()
module.fail_json(msg="Failed to write to file %s: %s" % (path, str(e)))
except (IOError, OSError) as e:
module.fail_json(msg="Failed to write to file %s: %s" % (path, to_native(e)))
try:
outf.close()
@@ -185,10 +183,9 @@ def sanity_check(module, host, key, sshkeygen):
outf = tempfile.NamedTemporaryFile(mode='w+')
outf.write(key)
outf.flush()
except IOError:
e = get_exception()
except IOError as e:
module.fail_json(msg="Failed to write to temporary file %s: %s" %
(outf.name, str(e)))
(outf.name, to_native(e)))
sshkeygen_command = [sshkeygen, '-F', host, '-f', outf.name]
rc, stdout, stderr = module.run_command(sshkeygen_command, check_rc=True)
@@ -299,8 +296,7 @@ def compute_diff(path, found_line, replace_or_add, state, key):
}
try:
inf = open(path, "r")
except IOError:
e = get_exception()
except IOError as e:
if e.errno == errno.ENOENT:
diff['before_header'] = '/dev/null'
else:
@@ -331,5 +327,6 @@ def main():
results = enforce_state(module, module.params)
module.exit_json(**results)
if __name__ == '__main__':
main()

View File

@@ -43,7 +43,6 @@ import re
from subprocess import Popen, PIPE, call
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils._text import to_native
LOCALE_NORMALIZATION = {
@@ -228,9 +227,8 @@ def main():
apply_change(state, name)
else:
apply_change_ubuntu(state, name)
except EnvironmentError:
e = get_exception()
module.fail_json(msg=e.strerror, exitValue=e.errno)
except EnvironmentError as e:
module.fail_json(msg=to_native(e), exitValue=e.errno)
module.exit_json(name=name, changed=changed, msg="OK")

View File

@@ -128,7 +128,6 @@ import os
from ansible.module_utils.basic import AnsibleModule, get_platform
from ansible.module_utils.ismount import ismount
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.six import iteritems
from ansible.module_utils._text import to_native
@@ -653,9 +652,8 @@ def main():
if os.path.exists(name):
try:
os.rmdir(name)
except (OSError, IOError):
e = get_exception()
module.fail_json(msg="Error rmdir %s: %s" % (name, str(e)))
except (OSError, IOError) as e:
module.fail_json(msg="Error rmdir %s: %s" % (name, to_native(e)))
elif state == 'unmounted':
if ismount(name) or is_bind_mounted(module, linux_mounts, name):
if not module.check_mode:
@@ -670,10 +668,9 @@ def main():
if not os.path.exists(name) and not module.check_mode:
try:
os.makedirs(name)
except (OSError, IOError):
e = get_exception()
except (OSError, IOError) as e:
module.fail_json(
msg="Error making dir %s: %s" % (name, str(e)))
msg="Error making dir %s: %s" % (name, to_native(e)))
name, changed = set_mount(module, args)
res = 0

View File

@@ -255,7 +255,7 @@ dest:
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils._text import to_native
import os
import re
import time
@@ -360,11 +360,10 @@ class PamdService(object):
stringline += '\n'
self.load_rules_from_string(stringline.replace("\\\n", ""))
except IOError:
e = get_exception()
except IOError as e:
self.ansible.fail_json(msg='Unable to open/read PAM module \
file %s with error %s. And line %s' %
(self.fname, str(e), stringline))
(self.fname, to_native(e), stringline))
def load_rules_from_string(self, stringvalue):
for line in stringvalue.splitlines():
@@ -687,5 +686,6 @@ def main():
module.exit_json(changed=change, ansible_facts=facts)
if __name__ == '__main__':
main()

View File

@@ -229,7 +229,6 @@ import time
from ansible.module_utils._text import to_native
from ansible.module_utils.basic import load_platform_subclass, AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
try:
import spwd
@@ -298,9 +297,8 @@ class User(object):
if module.params['expires']:
try:
self.expires = time.gmtime(module.params['expires'])
except Exception:
e = get_exception()
module.fail_json(msg="Invalid expires time %s: %s" % (self.expires, e))
except Exception as e:
module.fail_json(msg="Invalid expires time %s: %s" % (self.expires, to_native(e)))
if module.params['ssh_key_file'] is not None:
self.ssh_file = module.params['ssh_key_file']
@@ -637,9 +635,8 @@ class User(object):
try:
os.mkdir(ssh_dir, int('0700', 8))
os.chown(ssh_dir, info[2], info[3])
except OSError:
e = get_exception()
return (1, '', 'Failed to create %s: %s' % (ssh_dir, str(e)))
except OSError as e:
return (1, '', 'Failed to create %s: %s' % (ssh_dir, to_native(e)))
if os.path.exists(ssh_key_file):
return (None, 'Key already exists', '')
cmd = [self.module.get_bin_path('ssh-keygen', True)]
@@ -709,15 +706,13 @@ class User(object):
if os.path.exists(skeleton):
try:
shutil.copytree(skeleton, path, symlinks=True)
except OSError:
e = get_exception()
self.module.exit_json(failed=True, msg="%s" % e)
except OSError as e:
self.module.exit_json(failed=True, msg="%s" % to_native(e))
else:
try:
os.makedirs(path)
except OSError:
e = get_exception()
self.module.exit_json(failed=True, msg="%s" % e)
except OSError as e:
self.module.exit_json(failed=True, msg="%s" % to_native(e))
def chown_homedir(self, uid, gid, path):
try:
@@ -727,9 +722,8 @@ class User(object):
os.chown(os.path.join(root, d), uid, gid)
for f in files:
os.chown(os.path.join(root, f), uid, gid)
except OSError:
e = get_exception()
self.module.exit_json(failed=True, msg="%s" % e)
except OSError as e:
self.module.exit_json(failed=True, msg="%s" % to_native(e))
# ===========================================
@@ -1289,9 +1283,8 @@ class SunOS(User):
maxweeks = value.rstrip('\n')
elif key == "WARNWEEKS":
warnweeks = value.rstrip('\n')
except Exception:
err = get_exception()
self.module.fail_json(msg="failed to read /etc/default/passwd: %s" % str(err))
except Exception as err:
self.module.fail_json(msg="failed to read /etc/default/passwd: %s" % to_native(err))
return (minweeks, maxweeks, warnweeks)
@@ -1372,9 +1365,8 @@ class SunOS(User):
line = ':'.join(fields)
lines.append('%s\n' % line)
open(self.SHADOWFILE, 'w+').writelines(lines)
except Exception:
err = get_exception()
self.module.fail_json(msg="failed to update users password: %s" % str(err))
except Exception as err:
self.module.fail_json(msg="failed to update users password: %s" % to_native(err))
return (rc, out, err)
@@ -1468,9 +1460,8 @@ class SunOS(User):
lines.append('%s\n' % line)
open(self.SHADOWFILE, 'w+').writelines(lines)
rc = 0
except Exception:
err = get_exception()
self.module.fail_json(msg="failed to update users password: %s" % str(err))
except Exception as err:
self.module.fail_json(msg="failed to update users password: %s" % to_native(err))
return (rc, out, err)