This Task automates the installation and configuration of MySQL or PostgreSQL using Terraform and Ansible. It sets up the database server on an AWS EC2 instance, provisions necessary resources, and configures the database with users and permissions.
├── terraform/ │ ├── main.tf │ ├── variables.tf │ ├── outputs.tf │ ├── terraform.tfvars │ └── provider.tf ├── ansible/ │ ├── inventory.ini │ ├── playbook.yml │ ├── roles/ │ │ ├── db_install/ │ │ │ ├── tasks/main.yml │ │ │ ├── handlers/main.yml │ │ │ ├── templates/ │ │ │ ├── files/ │ │ │ └── vars/main.yml │ │ ├── db_config/ │ │ │ ├── tasks/main.yml │ │ │ ├── handlers/main.yml │ │ │ ├── templates/ │ │ │ ├── files/ │ │ │ └── vars/main.yml └── README.md
Use Terraform to:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "db_server" {
ami = "ami-12345678" # Ubuntu 20.04 LTS
instance_type = "t2.medium"
security_groups = [aws_security_group.db_sg.name]
key_name = "db-key-pair"
tags = {
Name = "DatabaseServer"
}
}
resource "aws_security_group" "db_sg" {
name = "db_security_group"
description = "Allow MySQL/PostgreSQL access"
ingress {
from_port = 3306 # Use 5432 for PostgreSQL
to_port = 3306
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Restrict to known IPs
}
}
terraform init
terraform apply -auto-approve
Once Terraform provisions the EC2 instance, Ansible is used for:
[database]
db_server ansible_host= ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa
- name: Configure Database Server
hosts: database
become: true
roles:
- db_install
- db_config
(roles/db_install/tasks/main.yml)
- name: Install MySQL Server
apt:
name: mysql-server
state: present
when: "'mysql' in ansible_facts['distribution']"
- name: Install PostgreSQL Server
apt:
name: postgresql
state: present
when: "'postgresql' in ansible_facts['distribution']"
- name: Ensure MySQL is running
service:
name: mysql
state: started
enabled: yes
when: "'mysql' in ansible_facts['distribution']"
- name: Ensure PostgreSQL is running
service:
name: postgresql
state: started
enabled: yes
when: "'postgresql' in ansible_facts['distribution']"
- name: Create Database
mysql_db:
name: my_database
state: present
when: "'mysql' in ansible_facts['distribution']"
- name: Create PostgreSQL Database
postgresql_db:
name: my_database
state: present
when: "'postgresql' in ansible_facts['distribution']"
- name: Create Database User
mysql_user:
name: db_user
password: db_pass
priv: "my_database.*:ALL"
host: "%"
state: present
when: "'mysql' in ansible_facts['distribution']"
- name: Create PostgreSQL User
postgresql_user:
db: my_database
name: db_user
password: db_pass
state: present
when: "'postgresql' in ansible_facts['distribution']"
ansible-playbook -i inventory.ini playbook.yml
ansible-playbook -i inventory.ini playbook.yml
This project provides an end-to-end automated database setup using Terraform (for infrastructure) and Ansible (for configuration). It installs either MySQL or PostgreSQL, creates databases, users, and permissions, and secures access.