Pulling MySQL Table/Field data for Jinja templates

62 views
Skip to first unread message

Gilberto Valentin

unread,
Mar 3, 2017, 2:20:38 PM3/3/17
to Ansible Project
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

Kai Stian Olstad

unread,
Mar 3, 2017, 3:03:33 PM3/3/17
to ansible...@googlegroups.com
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)

--
Kai Stian Olstad

Gilberto Valentin

unread,
Mar 4, 2017, 10:55:43 AM3/4/17
to Ansible Project, ansible-pr...@olstad.com
Thanks for the info...I'll definitely try this out :)
Reply all
Reply to author
Forward
0 new messages