fix failing fail_json call in postgresql_schema (#21495)

fix failing fail_json call in postgresql_schema

- Bugfix Pull Request

modules/database/postgresql/postgresql_schema

```
2.3.0
```

Here's an example of the error that was coming out. Massaged some linebreaks and backslashes to make it more readable:

    "module_stderr": "Traceback (most recent call last):
      File "/tmp/ansible_3X05GE/ansible_module_postgresql_schema.py", line 274, in <module>
        main()
      File "/tmp/ansible_3X05GE/ansible_module_postgresql_schema.py", line 265, in main
        module.fail_json(msg="Database query failed: %s" %(text, str(e)))
      NameError: global name 'text' is not defined
    ",

Now it triggers with the correct exception and shows the traceback. This duplication of str(e) and traceback seems to be the best design pattern.

Sample of the new output:

    An exception occurred during task execution. The full traceback is:
    Traceback (most recent call last):
      File "/tmp/ansible_gp4v1Q/ansible_module_postgresql_schema.py", line 254, in main
        changed = schema_create(cursor, schema, owner)
    ...
        return super(DictCursor, self).execute(query, vars)
    ProgrammingError: permission denied for database schemadb

    fatal: [localhost]: FAILED! => {
        "changed": false,
        "failed": true,
    ...
        },
        "msg": "Database query failed: permission denied for database schemadb\n"
This commit is contained in:
Ted Timmons 2017-02-16 11:26:40 -08:00 committed by Toshio Kuratomi
parent 3ff2c471b2
commit a000594436

View file

@ -101,7 +101,6 @@ schema:
sample: "acme"
'''
try:
import psycopg2
import psycopg2.extras
@ -110,6 +109,10 @@ except ImportError:
else:
postgresqldb_found = True
import traceback
from ansible.module_utils._text import to_native
class NotSupportedError(Exception):
pass
@ -231,7 +234,7 @@ def main():
cursor_factory=psycopg2.extras.DictCursor)
except Exception:
e = get_exception()
module.fail_json(msg="unable to connect to database: %s" %(text, str(e)))
module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc())
try:
if module.check_mode:
@ -262,7 +265,7 @@ def main():
raise
except Exception:
e = get_exception()
module.fail_json(msg="Database query failed: %s" %(text, str(e)))
module.fail_json(msg="Database query failed: %s" % to_native(e), exception=traceback.format_exc())
module.exit_json(changed=changed, schema=schema)