Operations 4 min read

Using Ansible Playbooks to Automate MySQL Installation and Cron Job Creation

This tutorial explains how to write Ansible Playbooks in YAML to automate complex tasks such as batch installing MySQL on remote servers and creating scheduled cron jobs, including code examples, parameter explanations, execution commands, and result verification.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Using Ansible Playbooks to Automate MySQL Installation and Cron Job Creation

Ansible Playbooks are defined using YAML files, which support lists, maps, and scalar values and are identified by the .yaml extension. Playbooks enable the automation of repetitive and complex tasks by allowing variables, conditions, loops, templates, roles, and includes.

Example 1: Batch Install MySQL Database

Write the following YAML Playbook:

- hosts: webserver
   remote_user: root
   tasks:
     - name: mysql-server install
       yum: name=mysql-server state=present

Key parameters: hosts: webserver – target host or group. remote_user: root – user for remote execution. tasks – list of actions. - name: mysql-server install – descriptive task name. yum: name=mysql-server state=present – installs the MySQL package. state=absent – would remove the package.

Execute and verify:

ansible all -a "/bin/rpm -q mysql-server"
ansible-playbook mysql-server.yaml

The output shows ok=2 changed=1, confirming successful installation. Additional checks can be run with:

ansible all -a "/bin/rpm -q mysql-server"
ansible all -a "/sbin/service mysqld start"

Example 2: Create a Cron Job via Playbook

Write the following YAML Playbook:

- hosts: webserver
   remote_user: root
   tasks:
     - name: crontab
       cron: name="testjob" day='10' job="sh /opt/backup.sh"

Run the playbook: ansible-playbook crond.yaml The result ok=2 changed=1 indicates the cron job was created successfully. Verify with:

ansible all -a 'crontab -l'   # list crontab entries on each node

Additional notes: use ansible-doc -l to list built‑in modules and ansible-doc <module_name> to view detailed usage of a specific module.

Thank you for reading; feel free to leave comments or share with others.

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.

AutomationOperationsmysqlYAMLcronAnsiblePlaybook
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

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.