In this tutorial, we'll explain how install database with Ansible.
Here's a detailed step-by-step guide on Automating Database Configurations with Ansible. This guide covers using Ansible to automate database setups for MySQL, PostgreSQL. It includes examples of using Ansible modules for database management.
1. Introduction
Ansible is a powerful automation tool that enables infrastructure as code, allowing you to manage and configure servers, networks, and databases efficiently. This guide will demonstrate how to automate the setup and configuration of popular databases like MySQL, PostgreSQL using Ansible.
Prerequisites
- Ubuntu 24.04 installed dedicated server or KVM VPS.
- 1 control server.
- 2 target servers.
- A basic understanding of YAML and Ansible playbooks.
Install Database with Ansible
2. Install Ansible
First, let's install Ansible using following command:
sudo apt update
sudo apt install ansible
SSH Access to the target database servers. Ansible uses SSH to connect to remote servers.
Root or Sudo Privileges on the database servers.
Python installed on the target servers, as Ansible requires Python to execute commands.
3: Set Up SSH Access
We need to setup a SSH access between the remote servers to ensure you can SSH into your managed nodes without a password prompt. This can be achieved by setting up SSH keys.
Generate SSH Keys (if not already done)
On your system, generate an SSH key pair (if you don’t have one already):
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This will create a pair of files:
- ~/.ssh/id_rsa (private key)
- ~/.ssh/id_rsa.pub (public key)
Copy the Public Key to Remote Servers
You need to copy the public key to the remote servers to enable passwordless SSH access. Use the ssh-copy-id command:
ssh-copy-id username@remote_server_ip
Replace username
with the remote user’s name and remote_server_ip
with the IP address of your remote server. Repeat this step for each remote server.
Test SSH access to ensure you can connect without a password:
ssh username@remote_server_ip
If you can connect without being prompted for a password, SSH access is correctly set up. Now exit the current connection and back to main server.
4. Setting Up an Ansible Inventory File
Create an inventory file that lists the database servers. For example:
mkdir -p ~/ansible/database_inventory && cd ansible
nano database_inventory/hosts
Add following:
[mysql_servers]
192.168.6.55
[postgresql_servers]
192.168.2.11
Replace the IPs with your server IPs
5. Creating a Playbook to Install Database Software
A playbook is a YAML file that defines the tasks Ansible should execute. We'll create separate playbooks for MySQL, PostgreSQL, and MongoDB installations.
a. MySQL Installation Playbook
Create a playbook file named install_mysql.yml
:
nano install_mysql.yml
Add following content:
---
- name: Install and configure MySQL
hosts: mysql_servers
become: yes
tasks:
- name: Install MySQL server
ansible.builtin.yum:
name: mysql-server
state: present
when: ansible_os_family == 'RedHat'
- name: Install MySQL server (Debian/Ubuntu)
ansible.builtin.apt:
name: mysql-server
state: present
when: ansible_os_family == 'Debian'
- name: Start MySQL service
ansible.builtin.service:
name: mysql
state: started
enabled: yes
b. PostgreSQL Installation Playbook
Create a playbook file named install_postgresql.yml
:
nano install_postgresql.yml
Add following content:
---
- name: Install and configure PostgreSQL
hosts: postgresql_servers
become: yes
tasks:
- name: Install PostgreSQL
ansible.builtin.yum:
name: postgresql-server
state: present
when: ansible_os_family == 'RedHat'
- name: Install PostgreSQL (Debian/Ubuntu)
ansible.builtin.apt:
name: postgresql
state: present
when: ansible_os_family == 'Debian'
- name: Initialize PostgreSQL database (RedHat)
ansible.builtin.command: postgresql-setup initdb
when: ansible_os_family == 'RedHat'
- name: Start PostgreSQL service
ansible.builtin.service:
name: postgresql
state: started
enabled: yes
6. Install databases
Now let's execute the playbooks
ansible-playbook -i database_inventory/hosts install_mysql.yml
ansible-playbook -i database_inventory/hosts install_postgresql.yml
You can verify that the databases are installed in the targeted server.
7. Conclusion
By following this guide, you've seen how install database with Ansible.
Automation with Ansible not only saves time but also reduces errors, making database management consistent and scalable. Consider expanding your automation further by creating roles, implementing monitoring solutions, or integrating your Ansible playbooks with CI/CD pipelines for continuous deployment.