Operations 4 min read

Step-by-Step Guide to Deploy MySQL Using Ansible Roles

This tutorial walks through creating an Ansible role that structures directories, defines configuration templates, sets variables, writes task files, and runs a playbook to install MySQL 8.0.32 on a Linux host, illustrating a complete automation workflow for database deployment.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Step-by-Step Guide to Deploy MySQL Using Ansible Roles

The article demonstrates how to build an Ansible role for installing MySQL 8.0.32 on a Linux server, covering directory layout, template creation, variable definition, task implementation, and playbook execution.

First, a standard role directory is created with

mkdir -p mysql/{default,files,handlers,meta,tasks,templates,vars}

and the resulting tree looks like:

mysql
├── default
├── files
│   └── mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
├── handlers
├── meta
├── tasks
│   ├── copy.yml
│   ├── install.yml
│   └── main.yml
├── templates
│   ├── my.cnf.j2
│   └── mysql_install.sh.j2
└── vars
    └── main.yml

In the templates folder, two Jinja2 files are added: my.cnf.j2 for MySQL configuration and mysql_install.sh.j2 for the installation script.

The role is invoked from a playbook mysql_install.yml:

---
- hosts: ms
  remote_user: root
  roles:
    - role: mysql

Variable values are stored in vars/main.yml:

mysql_version: mysql-8.0.32-linux-glibc2.12-x86_64
install_dir: /usr/local/mysql
data_dir: /data/mysql/mysql
source_dir: /tmp

Task files are created under tasks. install.yml runs the installation script:

- name: install mysql
  shell: bash {{source_dir}}/mysql_install.sh

The tasks/main.yml includes the copy and install tasks:

- include: copy.yml
- include: install.yml

Finally, the role is tested with a dry‑run command: ansible-playbook -C mysql_install.yml Overall, the guide provides a reproducible, modular approach to automate MySQL deployment using Ansible.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationDevOpsmysqlYAMLAnsible
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.