Ansible - mysql_db module
by
Jeremy Canfield |
Updated: October 14 2022
| Ansible articles
If you are not familiar with modules, check out Ansible - Getting Started with Modules.
The mysql_db module is used to do various things against a mySQL or MariaDB database, such as creating a database, dropping a database, dumping a database, and so on. In this example, all databases will be dumped to a file named all.sql.
---
- hosts: all
tasks:
- name: dump all databases to /tmp/all.sql
mysql_db:
state: dump
name: all
target: /tmp/all.sql
login_unix_socket: /var/lib/mysql/mysql.sock
...
If the following is returned, use the yum module to install epel-release and python-pip, and then use the pip module to install pymysql.
fatal: [server1.example.com]: FAILED! => {"changed": false, "msg": "The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) module is required."}
The following can be done to import a .sql dump.
---
- hosts: all
tasks:
- name: restore all databases using /tmp/all.sql
mysql_db:
state: import
name: all
target: /tmp/all.sql
login_unix_socket: /var/lib/mysql/mysql.sock
...
Did you find this article helpful?
If so, consider buying me a coffee over at