18 February 2026

mysql ansible automation linux sles

Install MySQL 8 Community Edition with Ansible

Quick one-liner: Automate MySQL 8 Community Edition installation with Ansible — set a known root password, skip the interactive prompts, and get a clean, repeatable deployment.


🤔 Why This Matters

Random passwords are annoying for automation. When you install MySQL interactively, it generates a temporary root password in the logs. You have to find it, reset it, and hope it doesn't change on the next install.

With Ansible, you control the root password from the start. One playbook, one command, MySQL is ready with the credentials you chose.


📋 The Playbook

Create mysql.yml:

---
- name: Install MySQL 8 Community Edition
  hosts: mysql
  become: yes
  vars:
    mysql_root_password: "C0mpl3XP@s5"

  tasks:
    - name: Install MySQL 8 Community
      zypper:
        name: mysql-community-server
        state: present

    - name: Start and enable MySQL service
      systemd:
        name: mysql
        state: started
        enabled: yes

    - name: Set root password
      mysql_user:
        name: root
        password: "{{ mysql_root_password }}"
        login_unix_socket: /var/run/mysql/mysql.sock
        state: present

Adjust the mysql_root_password variable to your preferred password.


🔧 Run the Playbook

ansible-playbook mysql.yml

Assuming your inventory has a host called mysql, you'll have MySQL 8 Community installed and configured in a few minutes.


🔧 Prerequisites

On the target host:

# Install Ansible MySQL module dependencies
sudo zypper install python3-PyMySQL

On the Ansible controller:

# Ensure the mysql Python module is available
pip3 install pymysql

The mysql_user module requires the PyMySQL library on both the controller and the target host.


✅ Verify the Installation

SSH into the MySQL host and test the connection:

mysql -u root -p'C0mpl3XP@s5' -e "SELECT VERSION();"

You should see:

+-----------+
| VERSION() |
+-----------+
| 8.0.xx    |
+-----------+

✅ Quick Reference

Command What It Does
ansible-playbook mysql.yml Install and configure MySQL 8
mysql -u root -p'password' Connect as root
systemctl status mysql Check MySQL service status
mysql -e "SHOW DATABASES;" List all databases