# Pulling MySQL Table/Field data for Jinja templates

**URL:** <https://forum.ansible.com/t/pulling-mysql-table-field-data-for-jinja-templates/23879>\
**Category:** Ansible Project\
**Created:** [March 3, 2017, 6:51pm UTC](https://forum.ansible.com/t/pulling-mysql-table-field-data-for-jinja-templates/23879 "2017-03-03T18:51:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [March 3, 2017, 6:51pm UTC](https://forum.ansible.com/t/pulling-mysql-table-field-data-for-jinja-templates/23879/1 "2017-03-03T18:51:33Z")

</div>

Hello, I am interested in pulling specific data from a table/field and apply that data to a .j2 template. For example, if I have a license key in a table on a field called RedHat license, I want to pull that license value and have my .j2 template use it for the creation of an XML template. I was pointed to the lookup plugin ([https://github.com/ansible/ansible/tree/devel/lib/ansible/plugins/lookup](https://github.com/ansible/ansible/tree/devel/lib/ansible/plugins/lookup)) but there isn’t anything there for MySQL. There is one for MongoDB, though. The issue is that I don’t know how I would use these plugins and I don’t know how to write plugins yet or how I would include one for the lookup plugin. What would be the best approach in my case? If I need to write my own plugin, is there a document that can guide me on this and how I would incorporate it with lookup? Thanks

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 3, 2017, 8:03pm UTC](https://forum.ansible.com/t/pulling-mysql-table-field-data-for-jinja-templates/23879/2 "2017-03-03T20:03:11Z")

</div>

You could use the command module and run an mysql query.

- name: Run a playbook  
&nbsp;&nbsp;&nbsp;command: mysql -u \<user\> -p\<passwd\> --silent --skip-column-names --execute="SELECT \<column\> FROM \<table\>"  
&nbsp;&nbsp;&nbsp;register: result

Then you can use the variable result.stdout and/or result.stdout\_lines to get the result.

(I don't recommend using -p\<passwd\>, use a option file instead, but easier to show a -p on the command line)

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [March 4, 2017, 3:55pm UTC](https://forum.ansible.com/t/pulling-mysql-table-field-data-for-jinja-templates/23879/3 "2017-03-04T15:55:43Z")

</div>

Thanks for the info…I’ll definitely try this out 🙂
