I have a very simple module to query sqlite database. it works except that the sqlite3.Row does not seem to be functional.
if anyone has run into similar issues please let me know I'm using python 3.6.8, ansible 4.9.0 <pre> PLAY [community.sqlite3 test playbook] ****************************************************************************************************************************************************************************************************** TASK [Test that my module works] ************************************************************************************************************************************************************************************************************ ok: [localhost] TASK [debug] ******************************************************************************************************************************************************************************************************************************** ok: [localhost] => { "msg": { "changed": false, "failed": false, "interpreter_version": "3.6.8 (default, Aug 13 2020, 07:46:32) \n[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]", "query_results": [ [ "152258fe-691b-4693-b768-2b1be1a30b96", "virtual_server", "virtual host running an os" ], [ "74183615-c732-455a-a1a1-591f2e940c9f", "physical_server", "physical host running an os" ] ] } } PLAY RECAP ********************************************************************************************************************************************************************************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 </pre> -- You received this message because you are subscribed to the Google Groups "Ansible Development" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-devel+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/69b9344f-6b0f-4068-a713-c4d9bba821bbn%40googlegroups.com.
#!/usr/bin/python3 import os import sys from ansible.module_utils.six.moves import configparser from ansible.module_utils._text import to_native from ansible.module_utils.basic import * try: import sqlite3 as sqlite3_driver except ImportError: sqlite3_driver = None sqlite3_driver_fail_msg = 'The sqlite3 does not appear to be part of your python distribution' def main(): module = AnsibleModule( argument_spec=dict( connection=dict(required=True, type='str'), query=dict(required=True, type='str'), named_arguments=dict(type='dict') ) ) connection = module.params['connection'] query = module.params['query'] if module.params.get('named_args'): args = module.params['named_arguments'] else: args = None if sqlite3_driver is None: module.fail_json(msg=sqlite3_driver_fail_msg) try: db_connection = sqlite3_driver.connect(connection) cursor = db_connection.cursor() db_connection.row_factory = sqlite3_driver.Row except Exception as e: module.fail_json(msg="unable to connect to database" "Exception message: %s" % (connection, to_native(e))) try: cursor.execute(query) except Exception as e: module.fail_json(msg="cannot execute SQL '%s' args [%s]: %s"%(query,arguments,to_native(e))) try: query_results = cursor.fetchall() except Exception as e: db_connection.rollback() module.fail_json(msg="Cannot fetch rows from cursor: %s" % to_native(e)) db_connection.commit() keywords = { 'changed': False, 'query_results': query_results , 'interpreter_version': sys.version } module.exit_json(**keywords) if __name__ == '__main__': main()