There should be some decent way to work with mysql databases using ansible like inserting data into tables or any command to run on mysql db.
I know there are modules to create db, manage replications, user and variables:
mysql_db - Add or remove MySQL databases from a remote host.mysql_replication (E) - Manage MySQL replicationmysql_user - Adds or removes a user from a MySQL database.mysql_variables - Manage MySQL global variablesMy use case scenario is, I've installed mysql-server on ubuntu and created the database successfully and now I have to insert data into the tables and wondering if there is a way to achieve it via ansible.
Solution 1:
I think you missed import functionality of mysql_db module. You can load both schema and data with it using import as parameter to state and giving it a file to load in target
Example from Ansible docs:
# Copy database dump file to remote host and restore it to database 'my_db'
- copy: src=dump.sql.bz2 dest=/tmp
- mysql_db: name=my_db state=import target=/tmp/dump.sql.bz2
Solution 2:
If mysql_db does not give you all options that you need and flexibility you can just use mysql program in combination with shell.
- name: Import DB 
  shell: mysql db_name < dump.sql
Above loads dump.sql file into database db_name. See mysql program manual for more options: man mysql
Solution 3:
mysqlimport utility with command module:
- name: Import DB 
  command: mysqlimport [options] db_name textfile1 [textfile2 ...]
See: mysqlimport docs
You can use mysql_db module. Its state: parameter has values of present, absent, dump, import. If it is set to import, then you can use target: parameter to indicate location, on the remote host, of the dump file to read from. Uncompressed SQL files (.sql) as well as bzip2 (.bz2) and gzip (.gz) compressed files are supported.
# Copy database dump file to remote host and restore it to database 'my_db'
- copy: src=dump.sql.bz2 dest=/tmp
- mysql_db: name=my_db state=import target=/tmp/dump.sql.bz2
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With