Fixed modules/system py files for 2.4 to 3.5 exceptions (#2367)

pull/18777/head
Chris Weber 8 years ago committed by Matt Clay
parent 868a4584a4
commit 891245c6f6

@ -61,6 +61,9 @@ EXAMPLES = '''
DEFAULT_LINK_PRIORITY = 50 DEFAULT_LINK_PRIORITY = 50
import re import re
from ansible.module_utils.basic import *
from ansible.module_utils.pycompat24 import get_exception
def main(): def main():
@ -135,12 +138,11 @@ def main():
) )
module.exit_json(changed=True) module.exit_json(changed=True)
except subprocess.CalledProcessError, cpe: except subprocess.CalledProcessError:
e = get_exception()
module.fail_json(msg=str(dir(cpe))) module.fail_json(msg=str(dir(cpe)))
else: else:
module.exit_json(changed=False) module.exit_json(changed=False)
# import module snippets
from ansible.module_utils.basic import *
main() main()

@ -106,6 +106,8 @@ import tempfile
import platform import platform
import pipes import pipes
import shlex import shlex
from ansible.module_utils.basic import *
from ansible.module_utils.pycompat24 import get_exception
CRONCMD = "/usr/bin/crontab" CRONCMD = "/usr/bin/crontab"
@ -147,7 +149,8 @@ class CronVar(object):
f = open(self.cron_file, 'r') f = open(self.cron_file, 'r')
self.lines = f.read().splitlines() self.lines = f.read().splitlines()
f.close() f.close()
except IOError, e: except IOError:
e = get_exception
# cron file does not exist # cron file does not exist
return return
except: except:
@ -203,7 +206,8 @@ class CronVar(object):
try: try:
os.unlink(self.cron_file) os.unlink(self.cron_file)
return True return True
except OSError, e: except OSError:
e = get_exception
# cron file does not exist # cron file does not exist
return False return False
except: except:
@ -425,7 +429,5 @@ def main():
# --- should never get here # --- should never get here
module.exit_json(msg="Unable to execute cronvar task.") module.exit_json(msg="Unable to execute cronvar task.")
# import module snippets
from ansible.module_utils.basic import *
main() main()

@ -82,6 +82,9 @@ EXAMPLES = '''
when: '/dev/mapper/luks-' in {{ item.device }} when: '/dev/mapper/luks-' in {{ item.device }}
''' '''
from ansible.module_utils.basic import *
from ansible.module_utils.pycompat24 import get_exception
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
@ -126,7 +129,8 @@ def main():
try: try:
crypttab = Crypttab(path) crypttab = Crypttab(path)
existing_line = crypttab.match(name) existing_line = crypttab.match(name)
except Exception, e: except Exception:
e = get_exception
module.fail_json(msg="failed to open and parse crypttab file: %s" % e, module.fail_json(msg="failed to open and parse crypttab file: %s" % e,
**module.params) **module.params)
@ -358,6 +362,4 @@ class Options(dict):
ret.append('%s=%s' % (k, v)) ret.append('%s=%s' % (k, v))
return ','.join(ret) return ','.join(ret)
# import module snippets
from ansible.module_utils.basic import *
main() main()

@ -80,6 +80,9 @@ EXAMPLES = '''
''' '''
from ansible.module_utils.basic import *
from ansible.module_utils.pycompat24 import get_exception
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
@ -110,7 +113,8 @@ def main():
try: try:
rc, out, err = module.run_command(cmd) rc, out, err = module.run_command(cmd)
except Exception, e: except Exception:
e = get_exception
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
msg = "Unexpected failure!" msg = "Unexpected failure!"
@ -136,8 +140,6 @@ def main():
module.fail_json(msg=msg) module.fail_json(msg=msg)
# import module snippets
from ansible.module_utils.basic import *
main() main()

@ -135,6 +135,8 @@ EXAMPLES = """
import shutil import shutil
import time import time
import socket import socket
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.basic import *
glusterbin = '' glusterbin = ''
@ -147,7 +149,8 @@ def run_gluster(gargs, **kwargs):
rc, out, err = module.run_command(args, **kwargs) rc, out, err = module.run_command(args, **kwargs)
if rc != 0: if rc != 0:
module.fail_json(msg='error running gluster (%s) command (rc=%d): %s' % (' '.join(args), rc, out or err)) module.fail_json(msg='error running gluster (%s) command (rc=%d): %s' % (' '.join(args), rc, out or err))
except Exception, e: except Exception:
e = get_exception
module.fail_json(msg='error running gluster (%s) command: %s' % (' '.join(args), str(e))) module.fail_json(msg='error running gluster (%s) command: %s' % (' '.join(args), str(e)))
return out return out
@ -456,6 +459,4 @@ def main():
module.exit_json(changed=changed, ansible_facts=facts) module.exit_json(changed=changed, ansible_facts=facts)
# import module snippets
from ansible.module_utils.basic import *
main() main()

@ -78,6 +78,8 @@ import os.path
import tempfile import tempfile
import errno import errno
import re import re
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.basic import *
def enforce_state(module, params): def enforce_state(module, params):
""" """
@ -121,7 +123,8 @@ def enforce_state(module, params):
if replace_or_add or found != (state=="present"): if replace_or_add or found != (state=="present"):
try: try:
inf=open(path,"r") inf=open(path,"r")
except IOError, e: except IOError:
e = get_exception
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
inf=None inf=None
else: else:
@ -139,7 +142,8 @@ def enforce_state(module, params):
outf.write(key) outf.write(key)
outf.flush() outf.flush()
module.atomic_move(outf.name,path) module.atomic_move(outf.name,path)
except (IOError,OSError),e: except (IOError,OSError):
e = get_exception()
module.fail_json(msg="Failed to write to file %s: %s" % \ module.fail_json(msg="Failed to write to file %s: %s" % \
(path,str(e))) (path,str(e)))
@ -173,7 +177,8 @@ def sanity_check(module,host,key,sshkeygen):
outf=tempfile.NamedTemporaryFile() outf=tempfile.NamedTemporaryFile()
outf.write(key) outf.write(key)
outf.flush() outf.flush()
except IOError,e: except IOError:
e = get_exception()
module.fail_json(msg="Failed to write to temporary file %s: %s" % \ module.fail_json(msg="Failed to write to temporary file %s: %s" % \
(outf.name,str(e))) (outf.name,str(e)))
rc,stdout,stderr=module.run_command([sshkeygen,'-F',host, rc,stdout,stderr=module.run_command([sshkeygen,'-F',host,
@ -224,7 +229,8 @@ def search_for_host_key(module,host,key,path,sshkeygen):
# This output format has been hardcoded in ssh-keygen since at least OpenSSH 4.0 # This output format has been hardcoded in ssh-keygen since at least OpenSSH 4.0
# It always outputs the non-localized comment before the found key # It always outputs the non-localized comment before the found key
found_line = int(re.search(r'found: line (\d+)', l).group(1)) found_line = int(re.search(r'found: line (\d+)', l).group(1))
except IndexError, e: except IndexError:
e = get_exception()
module.fail_json(msg="failed to parse output of ssh-keygen for line number: '%s'" % l) module.fail_json(msg="failed to parse output of ssh-keygen for line number: '%s'" % l)
else: else:
found_key = normalize_known_hosts_key(l,host) found_key = normalize_known_hosts_key(l,host)
@ -274,6 +280,4 @@ def main():
results = enforce_state(module,module.params) results = enforce_state(module,module.params)
module.exit_json(**results) module.exit_json(**results)
# import module snippets
from ansible.module_utils.basic import *
main() main()

@ -19,6 +19,7 @@ import os
import os.path import os.path
from subprocess import Popen, PIPE, call from subprocess import Popen, PIPE, call
import re import re
from ansible.module_utils.pycompat24 import get_exception
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
@ -64,6 +65,9 @@ LOCALE_NORMALIZATION = {
# location module specific support methods. # location module specific support methods.
# #
from ansible.module_utils.basic import *
from ansible.module_utils.pycompat24 import get_exception
def is_available(name, ubuntuMode): def is_available(name, ubuntuMode):
"""Check if the given locale is available on the system. This is done by """Check if the given locale is available on the system. This is done by
checking either : checking either :
@ -224,12 +228,11 @@ def main():
apply_change(state, name) apply_change(state, name)
else: else:
apply_change_ubuntu(state, name) apply_change_ubuntu(state, name)
except EnvironmentError, e: except EnvironmentError:
e = get_exception()
module.fail_json(msg=e.strerror, exitValue=e.errno) module.fail_json(msg=e.strerror, exitValue=e.errno)
module.exit_json(name=name, changed=changed, msg="OK") module.exit_json(name=name, changed=changed, msg="OK")
# import module snippets
from ansible.module_utils.basic import *
main() main()

@ -57,6 +57,8 @@ EXAMPLES = '''
- modprobe: name=dummy state=present params="numdummies=2" - modprobe: name=dummy state=present params="numdummies=2"
''' '''
from ansible.module_utils.basic import *
from ansible.module_utils.pycompat24 import get_exception
import shlex import shlex
@ -87,7 +89,8 @@ def main():
present = True present = True
break break
modules.close() modules.close()
except IOError, e: except IOError:
e = get_exception()
module.fail_json(msg=str(e), **args) module.fail_json(msg=str(e), **args)
# Check only; don't modify # Check only; don't modify
@ -118,6 +121,4 @@ def main():
module.exit_json(**args) module.exit_json(**args)
# import module snippets
from ansible.module_utils.basic import *
main() main()

@ -84,6 +84,8 @@ EXAMPLES = '''
''' '''
import datetime import datetime
from ansible.module_utils.basic import *
from ansible.module_utils.pycompat24 import get_exception
# exceptions --------------------------------------------------------------- {{{ # exceptions --------------------------------------------------------------- {{{
class OSXDefaultsException(Exception): class OSXDefaultsException(Exception):
@ -275,7 +277,7 @@ class OSXDefaults(object):
# Handle absent state # Handle absent state
if self.state == "absent": if self.state == "absent":
print "Absent state detected!" print ("Absent state detected!")
if self.current_value is None: if self.current_value is None:
return False return False
if self.module.check_mode: if self.module.check_mode:
@ -376,10 +378,10 @@ def main():
array_add=array_add, value=value, state=state, path=path) array_add=array_add, value=value, state=state, path=path)
changed = defaults.run() changed = defaults.run()
module.exit_json(changed=changed) module.exit_json(changed=changed)
except OSXDefaultsException, e: except OSXDefaultsException:
e = get_exception()
module.fail_json(msg=e.message) module.fail_json(msg=e.message)
# /main ------------------------------------------------------------------- }}} # /main ------------------------------------------------------------------- }}}
from ansible.module_utils.basic import *
main() main()

@ -65,6 +65,8 @@ try:
HAVE_SEOBJECT = True HAVE_SEOBJECT = True
except ImportError: except ImportError:
pass pass
from ansible.module_utils.basic import *
from ansible.module_utils.pycompat24 import get_exception
def main(): def main():
@ -90,7 +92,8 @@ def main():
try: try:
permissive_domains = seobject.permissiveRecords(store) permissive_domains = seobject.permissiveRecords(store)
except ValueError, e: except ValueError:
e = get_exception()
module.fail_json(domain=domain, msg=str(e)) module.fail_json(domain=domain, msg=str(e))
# not supported on EL 6 # not supported on EL 6
@ -99,7 +102,8 @@ def main():
try: try:
all_domains = permissive_domains.get_all() all_domains = permissive_domains.get_all()
except ValueError, e: except ValueError:
e = get_exception()
module.fail_json(domain=domain, msg=str(e)) module.fail_json(domain=domain, msg=str(e))
if permissive: if permissive:
@ -107,7 +111,8 @@ def main():
if not module.check_mode: if not module.check_mode:
try: try:
permissive_domains.add(domain) permissive_domains.add(domain)
except ValueError, e: except ValueError:
e = get_exception()
module.fail_json(domain=domain, msg=str(e)) module.fail_json(domain=domain, msg=str(e))
changed = True changed = True
else: else:
@ -115,7 +120,8 @@ def main():
if not module.check_mode: if not module.check_mode:
try: try:
permissive_domains.delete(domain) permissive_domains.delete(domain)
except ValueError, e: except ValueError:
e = get_exception()
module.fail_json(domain=domain, msg=str(e)) module.fail_json(domain=domain, msg=str(e))
changed = True changed = True
@ -123,8 +129,5 @@ def main():
permissive=permissive, domain=domain) permissive=permissive, domain=domain)
#################################################
# import module snippets
from ansible.module_utils.basic import *
main() main()

@ -80,6 +80,8 @@ try:
except ImportError: except ImportError:
HAVE_SEOBJECT=False HAVE_SEOBJECT=False
from ansible.module_utils.basic import *
from ansible.module_utils.pycompat24 import get_exception
def semanage_port_exists(seport, port, proto): def semanage_port_exists(seport, port, proto):
""" Get the SELinux port type definition from policy. Return None if it does """ Get the SELinux port type definition from policy. Return None if it does
@ -141,15 +143,20 @@ def semanage_port_add(module, ports, proto, setype, do_reload, serange='s0', ses
seport.add(port, proto, serange, setype) seport.add(port, proto, serange, setype)
change = change or not exists change = change or not exists
except ValueError, e: except ValueError:
e = get_exception()
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e))) module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e)))
except IOError, e: except IOError:
e = get_exception()
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e))) module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e)))
except KeyError, e: except KeyError:
e = get_exception()
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e))) module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e)))
except OSError, e: except OSError:
e = get_exception()
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e))) module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e)))
except RuntimeError, e: except RuntimeError:
e = get_exception()
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e))) module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e)))
return change return change
@ -186,15 +193,20 @@ def semanage_port_del(module, ports, proto, do_reload, sestore=''):
seport.delete(port, proto) seport.delete(port, proto)
change = change or not exists change = change or not exists
except ValueError, e: except ValueError:
e = get_exception()
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e))) module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e)))
except IOError,e: except IOError:
e = get_exception()
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e))) module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e)))
except KeyError, e: except KeyError:
e = get_exception()
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e))) module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e)))
except OSError, e: except OSError:
e = get_exception()
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e))) module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e)))
except RuntimeError, e: except RuntimeError:
e = get_exception()
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e))) module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e)))
return change return change
@ -257,5 +269,4 @@ def main():
module.exit_json(**result) module.exit_json(**result)
from ansible.module_utils.basic import *
main() main()

@ -87,6 +87,8 @@ EXAMPLES = '''
import platform import platform
import shlex import shlex
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.basic import *
def _load_dist_subclass(cls, *args, **kwargs): def _load_dist_subclass(cls, *args, **kwargs):
''' '''
@ -152,7 +154,8 @@ class Svc(object):
if os.path.exists(self.src_full): if os.path.exists(self.src_full):
try: try:
os.symlink(self.src_full, self.svc_full) os.symlink(self.src_full, self.svc_full)
except OSError, e: except OSError:
e = get_exception()
self.module.fail_json(path=self.src_full, msg='Error while linking: %s' % str(e)) self.module.fail_json(path=self.src_full, msg='Error while linking: %s' % str(e))
else: else:
self.module.fail_json(msg="Could not find source for service to enable (%s)." % self.src_full) self.module.fail_json(msg="Could not find source for service to enable (%s)." % self.src_full)
@ -160,7 +163,8 @@ class Svc(object):
def disable(self): def disable(self):
try: try:
os.unlink(self.svc_full) os.unlink(self.svc_full)
except OSError, e: except OSError:
e = get_exception()
self.module.fail_json(path=self.svc_full, msg='Error while unlinking: %s' % str(e)) self.module.fail_json(path=self.svc_full, msg='Error while unlinking: %s' % str(e))
self.execute_command([self.svc_cmd,'-dx',self.src_full]) self.execute_command([self.svc_cmd,'-dx',self.src_full])
@ -221,7 +225,8 @@ class Svc(object):
def execute_command(self, cmd): def execute_command(self, cmd):
try: try:
(rc, out, err) = self.module.run_command(' '.join(cmd)) (rc, out, err) = self.module.run_command(' '.join(cmd))
except Exception, e: except Exception:
e = get_exception()
self.module.fail_json(msg="failed to execute: %s" % str(e)) self.module.fail_json(msg="failed to execute: %s" % str(e))
return (rc, out, err) return (rc, out, err)
@ -267,7 +272,8 @@ def main():
svc.enable() svc.enable()
else: else:
svc.disable() svc.disable()
except (OSError, IOError), e: except (OSError, IOError):
e = get_exception()
module.fail_json(msg="Could change service link: %s" % str(e)) module.fail_json(msg="Could change service link: %s" % str(e))
if state is not None and state != svc.state: if state is not None and state != svc.state:
@ -284,13 +290,13 @@ def main():
open(d_file, "a").close() open(d_file, "a").close()
else: else:
os.unlink(d_file) os.unlink(d_file)
except (OSError, IOError), e: except (OSError, IOError):
e = get_exception()
module.fail_json(msg="Could change downed file: %s " % (str(e))) module.fail_json(msg="Could change downed file: %s " % (str(e)))
module.exit_json(changed=changed, svc=svc.report()) module.exit_json(changed=changed, svc=svc.report())
# this is magic, not normal python include
from ansible.module_utils.basic import *
main() main()

Loading…
Cancel
Save