How to Containerize Ansible for Automated MySQL Backups
This article demonstrates how to package Ansible in a Docker container, use the mysql_db module to create MySQL backups, and run a simple playbook, highlighting the benefits of containerized deployment for clean, flexible operations automation.
Ansible Overview
Ansible is a Python‑based automation tool that requires no client agents, making it lightweight and easy to manage. The article uses the mysql_db module to perform MySQL backups.
Why Use a Container?
Deploying Ansible inside a Docker container avoids polluting the host system with dependencies, simplifies migration across environments, and keeps the operational footprint minimal.
Environment Preparation
The author prefers not to install Ansible via yum on a Red Hat host because maintaining offline yum repositories and the large dependency chain can be cumbersome. Instead, a custom Docker image is built.
Dockerfile
FROM debian:latest
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list \
&& apt-get update -y \
&& apt-get upgrade -y \
&& apt-get install ansible -y \
&& apt-get install sshpass -y \
&& apt-get install pip -y \
&& apt-get install libmysql++-dev -y \
&& apt-get install default-mysql-client -y \
&& apt-get autoclean
RUN pip3 install mysqlclient
RUN mkdir -p /etc/ansible/
CMD ["/bin/bash"]The resulting image is about 900 MB and includes Ansible 2.10.8.
mysql_db Module Parameters
connect_timeout (integer): Connection timeout, default 30.
dump_extra_args (string): Extra arguments passed to mysqldump when state=dump.
encoding (string): Character set for the dump, e.g., utf8.
force (boolean): Continue on SQL errors (default no).
hex_blob (boolean): Dump binary columns as hex (default no).
ignore_tables (list): Tables to exclude from the dump.
login_host (string): Host running MySQL (default localhost).
login_user (string): Username for authentication.
login_password (string): Password for authentication.
login_port (integer): MySQL port (default 3306).
master_data (integer): Include master replication data (0‑2, default 0).
name (list, required): Database name(s) to dump.
quick (boolean): Use quick dump for large tables (default yes).
restrict_config_file (boolean): Read only the specified config file.
single_transaction (boolean): Perform dump in a single transaction.
skip_lock_tables (boolean): Skip table locking during dump.
state (string): Desired action – absent, dump, import, or present (default).
target (path): Remote path for the dump file; supports compressed formats.
Playbook Example
---
- hosts: 127.0.0.1
tasks:
- name: "mysql dump test"
mysql_db:
login_host: 192.168.43.51
login_user: root
login_password: ******
state: dump
name: test
target: /tmp/test_{{ ansible_date_time.date }}.gzPlaybook Execution Output
PLAYBOOK: bak.yml ****************************************************************
1 plays in bak.yml
PLAY [127.0.0.1] ****************************************************************
TASK [Gathering Facts] *********************************************************
ok: [127.0.0.1]
META: ran handlers
TASK [mysql dump test] *********************************************************
changed: [127.0.0.1] => {"changed": true, "db": "test", "db_list": ["test"], "executed_commands": ["/usr/bin/mysqldump --user=root --password=***** --host=192.168.43.51 --port=3306 test --quick | /bin/gzip > /tmp/test_2022-05-06.gz"], "msg": ""}
META: ran handlers
PLAY RECAP *********************************************************************
127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0Conclusion and Outlook
Containerizing Ansible provides a lightweight, portable way to manage operations such as MySQL backups, allowing teams to focus on the automation logic rather than environment setup. For larger‑scale scenarios, the author suggests adopting Ansible Tower or its open‑source counterpart AWX.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
