networklore / ansible-snmp

SNMP modules for Ansible
Other
8 stars 6 forks source link

Not Compatible with Python3 #4

Open hisaza opened 5 years ago

hisaza commented 5 years ago

I was testing snmp_device_version.py with python3 on Ansible and it would throw this error:

The full traceback is:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ansible/executor/task_executor.py", line 140, in run
    res = self._execute()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ansible/executor/task_executor.py", line 612, in _execute
    result = self._handler.run(task_vars=variables)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ansible/plugins/action/normal.py", line 46, in run
    result = merge_hash(result, self._execute_module(task_vars=task_vars, wrap_async=wrap_async))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ansible/plugins/action/__init__.py", line 742, in _execute_module
    (module_style, shebang, module_data, module_path) = self._configure_module(module_name=module_name, module_args=module_args, task_vars=task_vars)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ansible/plugins/action/__init__.py", line 178, in _configure_module
    environment=final_environment)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ansible/executor/module_common.py", line 973, in modify_module
    environment=environment)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ansible/executor/module_common.py", line 791, in _find_module_utils
    recursive_finder(module_name, b_module_data, py_module_names, py_module_cache, zf)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ansible/executor/module_common.py", line 538, in recursive_finder
    tree = ast.parse(data)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 157
    except Exception, err:
                    ^
SyntaxError: invalid syntax

fatal: [csr1]: FAILED! => {
    "msg": "Unexpected failure during module execution.",
    "stdout": ""
}

After changing the code in lines 157 and 163 to update to python3 syntax, it fixed the issue.

Python2 syntax:

try:
        dev = SnmpHandler(**nelsnmp_args)
    except Exception, err:
        module.fail_json(msg=str(err))

    hostinfo = HostInfo(dev)
    try:
        hostinfo.get_version()
    except Exception, err:
        module.fail_json(msg=str(err))

Python3 syntax:

try:
        dev = SnmpHandler(**nelsnmp_args)
    except Exception as err:
        module.fail_json(msg=str(err))

    hostinfo = HostInfo(dev)
    try:
        hostinfo.get_version()
    except Exception as err:
        module.fail_json(msg=str(err))