# Best practice for performing SQL query inside of Ansible playbook task?

**URL:** <https://forum.ansible.com/t/best-practice-for-performing-sql-query-inside-of-ansible-playbook-task/31841>\
**Category:** Ansible Project\
**Created:** [December 17, 2019, 10:38pm UTC](https://forum.ansible.com/t/best-practice-for-performing-sql-query-inside-of-ansible-playbook-task/31841 "2019-12-17T22:38:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruce\_W\_Mohler](https://avatars.discourse-cdn.com/v4/letter/b/d78d45/32.png) [@Bruce\_W\_Mohler](https://forum.ansible.com/u/Bruce_W_Mohler)\
**Post date:** [December 17, 2019, 10:38pm UTC](https://forum.ansible.com/t/best-practice-for-performing-sql-query-inside-of-ansible-playbook-task/31841/1 "2019-12-17T22:38:28Z")

</div>

What is the best practice for performing a SQL query inside of an Ansible playbook task?  
I know that Ansible provides some builtin MySQL modules but they only allow you to create databases, tables inside of databases, and users. They don’t allow you to actually do a query.  
There’s a mysql\_query.py library module ([https://github.com/teemops/ansible-modules/blob/master/database/mysql\_query.py](https://github.com/teemops/ansible-modules/blob/master/database/mysql_query.py)) written back in 2016 that worked up to the point until MySQL required you to authenticate to a UNIX socket rather than a TCP/IP socket.  
There’s a **newer** mysql\_query.py library module ([https://github.com/zauberpony/ansible-mysql-query/blob/master/library/mysql\_query.py](https://github.com/zauberpony/ansible-mysql-query/blob/master/library/mysql_query.py)) that does seem to handle UNIX socket authentication but the manner of expressing the queries is completely different (and non-intuitive to my eyes).  
Someone pointed to an obscure use of the mysql\_db module ([https://docs.ansible.com/ansible/latest/modules/mysql\_db\_module.html](https://docs.ansible.com/ansible/latest/modules/mysql_db_module.html) - see the last import example) do “run a query” but you need to have an external Jinja2 file for each query and I’d prefer something embedded, if possible.  
Then there’s this _mechanism_ ([https://stackoverflow.com/questions/30605950/running-a-select-query-with-an-ansible-task](https://stackoverflow.com/questions/30605950/running-a-select-query-with-an-ansible-task)) which I haven’t been able to test yet.

So what do people do? How do you handle this?  
What’s best practice?  
I would appreciate it if you would provide examples.  
Thank you!
