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.
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=presentKey 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.yamlThe 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 nodeAdditional 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.
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.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
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.
