Pulling MySQL Table/Field data for Jinja templates

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) 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

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

- name: Run a playbook
   command: mysql -u <user> -p<passwd> --silent --skip-column-names --execute="SELECT <column> FROM <table>"
   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)

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