Operations 19 min read

An Introduction to Ansible: Installation, Configuration, and MySQL Replication with Playbooks

This article introduces Ansible, a Python‑based configuration management and deployment tool, explains its installation prerequisites, demonstrates basic ad‑hoc commands, and provides a comprehensive guide to managing MySQL master‑slave replication using Ansible modules, playbooks, and role‑based directory structures.

Art of Distributed System Architecture Design
Art of Distributed System Architecture Design
Art of Distributed System Architecture Design
An Introduction to Ansible: Installation, Configuration, and MySQL Replication with Playbooks

Ansible is a Python‑based open‑source configuration management and application deployment tool created by Michael DeHaan in 2012, comparable to Chef, Puppet, and SaltStack.

It operates over SSH without requiring agents on target machines, and does not run persistent daemons or need a database.

Typical automation goals include application deployment, configuration management, continuous delivery, and cloud service management (e.g., AWS).

Supported users include Evernote, Rackspace, NASA, Atlassian, and Twitter.

Installation Prerequisites

The control node must run Python 2.6 or later on a supported OS (RedHat, Debian, CentOS, macOS, BSD, etc.). Managed nodes need Python 2.4+; if Python < 2.5, install python-simplejson , and if SELinux is enabled, install libselinux-python .

Installing Ansible

Although system package managers (yum, apt-get) can be used (yum requires EPEL on CentOS/RedHat), the author recommends installing via pip. First ensure python-setuptools is present:

yum -y install python-setuptools

Then install pip:

easy_install pip

Finally install Ansible:

pip install ansible

After these steps, Ansible is ready on the control server.

Ad‑hoc Command Example

Ansible stores inventory in /etc/ansible/hosts (INI‑style). Example inventory:

[webserver]
192.168.1.1
192.168.1.2
[databaseServer]
192.168.1.10
192.168.1.11

Running an ad‑hoc command to execute whoami on the webserver group:

ansible webserver -a "whoami"

The output confirms connectivity and command execution.

Managing MySQL Replication with Ansible Modules

The mysql_replication module can query master status, change master configuration, and start the slave. Example commands:

ansible mysql-master -m mysql_replication -a "login_user=root login_password=123456 mode=getmaster"
ansible mysql-slave -m mysql_replication -a "login_user=root login_password=123456 mode=changemaster master_host='172.16.64.147' master_user=repl master_password=123456 master_log_file=mysql.000003 master_log_pos=711"
ansible mysql-slave -m mysql_replication -a "login_user=root login_password=123456 mode=startslave"

JSON‑formatted results show binary log file, position, and replication status.

Using Playbooks for Reusable Automation

Playbooks, written in YAML, allow declarative, repeatable automation. The author organizes files using Ansible roles (defaults, handlers, meta, tasks, templates, vars). Example directory layout and role files are shown.

Key role files:

defaults/main.yml – defines MySQL variables such as port, bind address, root password, databases, users, and replication settings.

handlers/main.yml – defines a handler to restart MySQL.

tasks/main.yml – installs packages, copies my.cnf , starts services, configures users, and sets up replication.

templates/my.cnf.RedHat.j2 – Jinja2 template for MySQL configuration, with conditional sections for master or slave roles.

vars/RedHat.yml – lists required packages and service names for RedHat‑based systems.

The playbook mysql_repl.yml applies the mysql role to a master host and a slave host, passing role variables to configure replication.

- hosts: mysql-master
  roles:
    - {role: mysql, mysql_db: none, mysql_users: [{name: jack, pass: 123456, priv: "*.*:ALL"}], mysql_db_id: 1008 }

- hosts: mysql-slave
  roles:
    - {role: mysql, mysql_db: none, mysql_users: none, mysql_repl_role: slave,
       mysql_repl_master: 192.168.1.10, mysql_db_id:1009,
       mysql_repl_user: [{name: repl, pass: 123456}] }

Running ansible-playbook mysql_repl.yml executes the tasks in order, with a final PLAY RECAP summarising successes, changes, and failures.

Comparison with Other Configuration Management Tools

A brief table compares Ansible, Puppet, and SaltStack on language, agent requirement, extensibility, security, platform support, UI, configuration format, and command‑line capabilities.

Conclusion

Ansible has rapidly gained popularity, with over one million downloads and a top‑10 ranking on GitHub for Python projects. The article demonstrates Ansible’s capabilities through a MySQL replication use case, encouraging readers to adopt this powerful automation platform.

automationconfiguration managementdevopsMySQL replicationAnsiblePlaybooks
Art of Distributed System Architecture Design
Written by

Art of Distributed System Architecture Design

Introductions to large-scale distributed system architectures; insights and knowledge sharing on large-scale internet system architecture; front-end web architecture overviews; practical tips and experiences with PHP, JavaScript, Erlang, C/C++ and other languages in large-scale internet system development.

0 followers
Reader feedback

How this landed with the community

login 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.