From 891245c6f6a6e7f4e90d7167df352200cd6f6d24 Mon Sep 17 00:00:00 2001 From: Chris Weber Date: Fri, 3 Jun 2016 06:23:55 -0700 Subject: [PATCH] Fixed modules/system py files for 2.4 to 3.5 exceptions (#2367) --- .../modules/extras/system/alternatives.py | 8 +++-- lib/ansible/modules/extras/system/cronvar.py | 10 +++--- lib/ansible/modules/extras/system/crypttab.py | 8 +++-- lib/ansible/modules/extras/system/getent.py | 8 +++-- .../modules/extras/system/gluster_volume.py | 7 ++-- .../modules/extras/system/known_hosts.py | 16 +++++---- .../modules/extras/system/locale_gen.py | 9 +++-- lib/ansible/modules/extras/system/modprobe.py | 7 ++-- .../modules/extras/system/osx_defaults.py | 8 +++-- .../extras/system/selinux_permissive.py | 17 ++++++---- lib/ansible/modules/extras/system/seport.py | 33 ++++++++++++------- lib/ansible/modules/extras/system/svc.py | 20 +++++++---- 12 files changed, 95 insertions(+), 56 deletions(-) diff --git a/lib/ansible/modules/extras/system/alternatives.py b/lib/ansible/modules/extras/system/alternatives.py index e81a3e83065..c056348f1d5 100644 --- a/lib/ansible/modules/extras/system/alternatives.py +++ b/lib/ansible/modules/extras/system/alternatives.py @@ -61,6 +61,9 @@ EXAMPLES = ''' DEFAULT_LINK_PRIORITY = 50 import re +from ansible.module_utils.basic import * +from ansible.module_utils.pycompat24 import get_exception + def main(): @@ -135,12 +138,11 @@ def main(): ) module.exit_json(changed=True) - except subprocess.CalledProcessError, cpe: + except subprocess.CalledProcessError: + e = get_exception() module.fail_json(msg=str(dir(cpe))) else: module.exit_json(changed=False) -# import module snippets -from ansible.module_utils.basic import * main() diff --git a/lib/ansible/modules/extras/system/cronvar.py b/lib/ansible/modules/extras/system/cronvar.py index 1873c9d09c7..244bcd63da7 100644 --- a/lib/ansible/modules/extras/system/cronvar.py +++ b/lib/ansible/modules/extras/system/cronvar.py @@ -106,6 +106,8 @@ import tempfile import platform import pipes import shlex +from ansible.module_utils.basic import * +from ansible.module_utils.pycompat24 import get_exception CRONCMD = "/usr/bin/crontab" @@ -147,7 +149,8 @@ class CronVar(object): f = open(self.cron_file, 'r') self.lines = f.read().splitlines() f.close() - except IOError, e: + except IOError: + e = get_exception # cron file does not exist return except: @@ -203,7 +206,8 @@ class CronVar(object): try: os.unlink(self.cron_file) return True - except OSError, e: + except OSError: + e = get_exception # cron file does not exist return False except: @@ -425,7 +429,5 @@ def main(): # --- should never get here module.exit_json(msg="Unable to execute cronvar task.") -# import module snippets -from ansible.module_utils.basic import * main() diff --git a/lib/ansible/modules/extras/system/crypttab.py b/lib/ansible/modules/extras/system/crypttab.py index 842b5bc7d09..4b36200c526 100644 --- a/lib/ansible/modules/extras/system/crypttab.py +++ b/lib/ansible/modules/extras/system/crypttab.py @@ -82,6 +82,9 @@ EXAMPLES = ''' when: '/dev/mapper/luks-' in {{ item.device }} ''' +from ansible.module_utils.basic import * +from ansible.module_utils.pycompat24 import get_exception + def main(): module = AnsibleModule( @@ -126,7 +129,8 @@ def main(): try: crypttab = Crypttab(path) 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.params) @@ -358,6 +362,4 @@ class Options(dict): ret.append('%s=%s' % (k, v)) return ','.join(ret) -# import module snippets -from ansible.module_utils.basic import * main() diff --git a/lib/ansible/modules/extras/system/getent.py b/lib/ansible/modules/extras/system/getent.py index bb6d162398c..ba0d0061460 100644 --- a/lib/ansible/modules/extras/system/getent.py +++ b/lib/ansible/modules/extras/system/getent.py @@ -80,6 +80,9 @@ EXAMPLES = ''' ''' +from ansible.module_utils.basic import * +from ansible.module_utils.pycompat24 import get_exception + def main(): module = AnsibleModule( argument_spec = dict( @@ -110,7 +113,8 @@ def main(): try: rc, out, err = module.run_command(cmd) - except Exception, e: + except Exception: + e = get_exception module.fail_json(msg=str(e)) msg = "Unexpected failure!" @@ -136,8 +140,6 @@ def main(): module.fail_json(msg=msg) -# import module snippets -from ansible.module_utils.basic import * main() diff --git a/lib/ansible/modules/extras/system/gluster_volume.py b/lib/ansible/modules/extras/system/gluster_volume.py index e0b1ef01708..1f968271154 100644 --- a/lib/ansible/modules/extras/system/gluster_volume.py +++ b/lib/ansible/modules/extras/system/gluster_volume.py @@ -135,6 +135,8 @@ EXAMPLES = """ import shutil import time import socket +from ansible.module_utils.pycompat24 import get_exception +from ansible.module_utils.basic import * glusterbin = '' @@ -147,7 +149,8 @@ def run_gluster(gargs, **kwargs): rc, out, err = module.run_command(args, **kwargs) if rc != 0: 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))) return out @@ -456,6 +459,4 @@ def main(): module.exit_json(changed=changed, ansible_facts=facts) -# import module snippets -from ansible.module_utils.basic import * main() diff --git a/lib/ansible/modules/extras/system/known_hosts.py b/lib/ansible/modules/extras/system/known_hosts.py index d1890af5182..e7d9df59ccf 100644 --- a/lib/ansible/modules/extras/system/known_hosts.py +++ b/lib/ansible/modules/extras/system/known_hosts.py @@ -78,6 +78,8 @@ import os.path import tempfile import errno import re +from ansible.module_utils.pycompat24 import get_exception +from ansible.module_utils.basic import * def enforce_state(module, params): """ @@ -121,7 +123,8 @@ def enforce_state(module, params): if replace_or_add or found != (state=="present"): try: inf=open(path,"r") - except IOError, e: + except IOError: + e = get_exception if e.errno == errno.ENOENT: inf=None else: @@ -139,7 +142,8 @@ def enforce_state(module, params): outf.write(key) outf.flush() 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" % \ (path,str(e))) @@ -173,7 +177,8 @@ def sanity_check(module,host,key,sshkeygen): outf=tempfile.NamedTemporaryFile() outf.write(key) outf.flush() - except IOError,e: + except IOError: + e = get_exception() module.fail_json(msg="Failed to write to temporary file %s: %s" % \ (outf.name,str(e))) 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 # It always outputs the non-localized comment before the found key 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) else: found_key = normalize_known_hosts_key(l,host) @@ -274,6 +280,4 @@ def main(): results = enforce_state(module,module.params) module.exit_json(**results) -# import module snippets -from ansible.module_utils.basic import * main() diff --git a/lib/ansible/modules/extras/system/locale_gen.py b/lib/ansible/modules/extras/system/locale_gen.py index 9cb06ccc235..48cfb3ed8be 100644 --- a/lib/ansible/modules/extras/system/locale_gen.py +++ b/lib/ansible/modules/extras/system/locale_gen.py @@ -19,6 +19,7 @@ import os import os.path from subprocess import Popen, PIPE, call import re +from ansible.module_utils.pycompat24 import get_exception DOCUMENTATION = ''' --- @@ -64,6 +65,9 @@ LOCALE_NORMALIZATION = { # location module specific support methods. # +from ansible.module_utils.basic import * +from ansible.module_utils.pycompat24 import get_exception + def is_available(name, ubuntuMode): """Check if the given locale is available on the system. This is done by checking either : @@ -224,12 +228,11 @@ def main(): apply_change(state, name) else: apply_change_ubuntu(state, name) - except EnvironmentError, e: + except EnvironmentError: + e = get_exception() module.fail_json(msg=e.strerror, exitValue=e.errno) module.exit_json(name=name, changed=changed, msg="OK") -# import module snippets -from ansible.module_utils.basic import * main() diff --git a/lib/ansible/modules/extras/system/modprobe.py b/lib/ansible/modules/extras/system/modprobe.py index 405d5ea22c3..94c1a70437b 100644 --- a/lib/ansible/modules/extras/system/modprobe.py +++ b/lib/ansible/modules/extras/system/modprobe.py @@ -57,6 +57,8 @@ EXAMPLES = ''' - modprobe: name=dummy state=present params="numdummies=2" ''' +from ansible.module_utils.basic import * +from ansible.module_utils.pycompat24 import get_exception import shlex @@ -87,7 +89,8 @@ def main(): present = True break modules.close() - except IOError, e: + except IOError: + e = get_exception() module.fail_json(msg=str(e), **args) # Check only; don't modify @@ -118,6 +121,4 @@ def main(): module.exit_json(**args) -# import module snippets -from ansible.module_utils.basic import * main() diff --git a/lib/ansible/modules/extras/system/osx_defaults.py b/lib/ansible/modules/extras/system/osx_defaults.py index 7a04be455d3..126f621d883 100644 --- a/lib/ansible/modules/extras/system/osx_defaults.py +++ b/lib/ansible/modules/extras/system/osx_defaults.py @@ -84,6 +84,8 @@ EXAMPLES = ''' ''' import datetime +from ansible.module_utils.basic import * +from ansible.module_utils.pycompat24 import get_exception # exceptions --------------------------------------------------------------- {{{ class OSXDefaultsException(Exception): @@ -275,7 +277,7 @@ class OSXDefaults(object): # Handle absent state if self.state == "absent": - print "Absent state detected!" + print ("Absent state detected!") if self.current_value is None: return False if self.module.check_mode: @@ -376,10 +378,10 @@ def main(): array_add=array_add, value=value, state=state, path=path) changed = defaults.run() module.exit_json(changed=changed) - except OSXDefaultsException, e: + except OSXDefaultsException: + e = get_exception() module.fail_json(msg=e.message) # /main ------------------------------------------------------------------- }}} -from ansible.module_utils.basic import * main() diff --git a/lib/ansible/modules/extras/system/selinux_permissive.py b/lib/ansible/modules/extras/system/selinux_permissive.py index ec3575d9da4..df01b4333fd 100644 --- a/lib/ansible/modules/extras/system/selinux_permissive.py +++ b/lib/ansible/modules/extras/system/selinux_permissive.py @@ -65,6 +65,8 @@ try: HAVE_SEOBJECT = True except ImportError: pass +from ansible.module_utils.basic import * +from ansible.module_utils.pycompat24 import get_exception def main(): @@ -90,7 +92,8 @@ def main(): try: permissive_domains = seobject.permissiveRecords(store) - except ValueError, e: + except ValueError: + e = get_exception() module.fail_json(domain=domain, msg=str(e)) # not supported on EL 6 @@ -99,7 +102,8 @@ def main(): try: all_domains = permissive_domains.get_all() - except ValueError, e: + except ValueError: + e = get_exception() module.fail_json(domain=domain, msg=str(e)) if permissive: @@ -107,7 +111,8 @@ def main(): if not module.check_mode: try: permissive_domains.add(domain) - except ValueError, e: + except ValueError: + e = get_exception() module.fail_json(domain=domain, msg=str(e)) changed = True else: @@ -115,7 +120,8 @@ def main(): if not module.check_mode: try: permissive_domains.delete(domain) - except ValueError, e: + except ValueError: + e = get_exception() module.fail_json(domain=domain, msg=str(e)) changed = True @@ -123,8 +129,5 @@ def main(): permissive=permissive, domain=domain) -################################################# -# import module snippets -from ansible.module_utils.basic import * main() diff --git a/lib/ansible/modules/extras/system/seport.py b/lib/ansible/modules/extras/system/seport.py index 85918d8dac1..b9686095f88 100644 --- a/lib/ansible/modules/extras/system/seport.py +++ b/lib/ansible/modules/extras/system/seport.py @@ -80,6 +80,8 @@ try: except ImportError: HAVE_SEOBJECT=False +from ansible.module_utils.basic import * +from ansible.module_utils.pycompat24 import get_exception def semanage_port_exists(seport, port, proto): """ 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) 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))) - except IOError, e: + except IOError: + e = get_exception() 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))) - except OSError, e: + except OSError: + e = get_exception() 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))) return change @@ -186,15 +193,20 @@ def semanage_port_del(module, ports, proto, do_reload, sestore=''): seport.delete(port, proto) 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))) - except IOError,e: + except IOError: + e = get_exception() 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))) - except OSError, e: + except OSError: + e = get_exception() 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))) return change @@ -257,5 +269,4 @@ def main(): module.exit_json(**result) -from ansible.module_utils.basic import * main() diff --git a/lib/ansible/modules/extras/system/svc.py b/lib/ansible/modules/extras/system/svc.py index 6cc8c1d21ef..e82b0591d59 100755 --- a/lib/ansible/modules/extras/system/svc.py +++ b/lib/ansible/modules/extras/system/svc.py @@ -87,6 +87,8 @@ EXAMPLES = ''' import platform import shlex +from ansible.module_utils.pycompat24 import get_exception +from ansible.module_utils.basic import * def _load_dist_subclass(cls, *args, **kwargs): ''' @@ -152,7 +154,8 @@ class Svc(object): if os.path.exists(self.src_full): try: 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)) else: 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): try: 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.execute_command([self.svc_cmd,'-dx',self.src_full]) @@ -221,7 +225,8 @@ class Svc(object): def execute_command(self, cmd): try: (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)) return (rc, out, err) @@ -267,7 +272,8 @@ def main(): svc.enable() else: svc.disable() - except (OSError, IOError), e: + except (OSError, IOError): + e = get_exception() module.fail_json(msg="Could change service link: %s" % str(e)) if state is not None and state != svc.state: @@ -284,13 +290,13 @@ def main(): open(d_file, "a").close() else: 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.exit_json(changed=changed, svc=svc.report()) -# this is magic, not normal python include -from ansible.module_utils.basic import * + main()