123 lines
No EOL
4.2 KiB
YAML
123 lines
No EOL
4.2 KiB
YAML
---
|
|
- name: Setup MariaDB in Container
|
|
hosts: lab02
|
|
become: true
|
|
vars:
|
|
ct_id: 301
|
|
mariadb_root_password: "dOtPgrVnkIW1zwfjvW9rSw"
|
|
wavelog_password: "m4icNmGTRtOMgrNxsxEdrw"
|
|
|
|
tasks:
|
|
- name: Configure DNS in container
|
|
shell: |
|
|
pct exec {{ ct_id }} -- bash -c "echo 'nameserver 9.9.9.9' > /etc/resolv.conf"
|
|
|
|
- name: Update container packages
|
|
shell: pct exec {{ ct_id }} -- apt-get update
|
|
register: apt_update
|
|
retries: 3
|
|
delay: 5
|
|
until: apt_update.rc == 0
|
|
|
|
- name: Install MariaDB Server
|
|
shell: |
|
|
pct exec {{ ct_id }} -- bash -c "
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
apt-get install -y mariadb-server mariadb-client
|
|
"
|
|
|
|
- name: Start and enable MariaDB
|
|
shell: |
|
|
pct exec {{ ct_id }} -- systemctl enable mariadb
|
|
pct exec {{ ct_id }} -- systemctl start mariadb
|
|
|
|
- name: Secure MariaDB installation
|
|
shell: |
|
|
pct exec {{ ct_id }} -- bash -c "mysql -u root << 'EOF'
|
|
-- Set root password
|
|
ALTER USER 'root'@'localhost' IDENTIFIED BY '{{ mariadb_root_password }}';
|
|
|
|
-- Create root user for remote access
|
|
CREATE USER IF NOT EXISTS 'root'@'%' IDENTIFIED BY '{{ mariadb_root_password }}';
|
|
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
|
|
|
|
-- Remove anonymous users
|
|
DELETE FROM mysql.user WHERE User='';
|
|
|
|
-- Remove test database
|
|
DROP DATABASE IF EXISTS test;
|
|
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
|
|
|
|
-- Reload privileges
|
|
FLUSH PRIVILEGES;
|
|
EOF"
|
|
|
|
- name: Configure MariaDB for network access
|
|
shell: |
|
|
pct exec {{ ct_id }} -- sed -i 's/^bind-address.*=.*/bind-address = 0.0.0.0/' /etc/mysql/mariadb.conf.d/50-server.cnf
|
|
pct exec {{ ct_id }} -- systemctl restart mariadb
|
|
|
|
- name: Create Wavelog database and user
|
|
shell: |
|
|
pct exec {{ ct_id }} -- mysql -u root -p'{{ mariadb_root_password }}' << 'EOF'
|
|
CREATE DATABASE IF NOT EXISTS wavelog CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
|
CREATE USER IF NOT EXISTS 'wavelog'@'%' IDENTIFIED BY '{{ wavelog_password }}';
|
|
GRANT ALL PRIVILEGES ON wavelog.* TO 'wavelog'@'%';
|
|
FLUSH PRIVILEGES;
|
|
EOF
|
|
|
|
- name: Test database connections
|
|
shell: |
|
|
pct exec {{ ct_id }} -- mysql -u root -p'{{ mariadb_root_password }}' -e "SELECT VERSION();"
|
|
pct exec {{ ct_id }} -- mysql -u wavelog -p'{{ wavelog_password }}' wavelog -e "SELECT DATABASE();"
|
|
register: db_test
|
|
|
|
- name: Save all credentials
|
|
copy:
|
|
content: |
|
|
MariaDB Container Credentials
|
|
=============================
|
|
Created: {{ ansible_date_time.iso8601 }}
|
|
|
|
Container Details:
|
|
- Container ID: {{ ct_id }}
|
|
- Host: lab02
|
|
- IP Address: 10.0.19.6
|
|
- Port: 3306
|
|
|
|
Root Credentials:
|
|
- Username: root
|
|
- Password: {{ mariadb_root_password }}
|
|
- Connection: mysql -h 10.0.19.6 -u root -p'{{ mariadb_root_password }}'
|
|
|
|
Wavelog Database:
|
|
- Database: wavelog
|
|
- Username: wavelog
|
|
- Password: {{ wavelog_password }}
|
|
- Connection: mysql -h 10.0.19.6 -u wavelog -p'{{ wavelog_password }}' wavelog
|
|
|
|
Test Results:
|
|
{{ db_test.stdout }}
|
|
dest: "./mariadb_all_credentials.txt"
|
|
mode: '0600'
|
|
delegate_to: localhost
|
|
|
|
- name: Update Kubernetes secret for Wavelog
|
|
shell: |
|
|
kubectl create secret generic wavelog-db-secret \
|
|
--from-literal=password="{{ wavelog_password }}" \
|
|
--dry-run=client -o yaml | kubectl apply -f - -n wavelog
|
|
environment:
|
|
KUBECONFIG: "{{ playbook_dir }}/kubeconfig"
|
|
delegate_to: localhost
|
|
|
|
- name: Display summary
|
|
debug:
|
|
msg: |
|
|
MariaDB setup completed successfully!
|
|
|
|
Root password: {{ mariadb_root_password }}
|
|
Wavelog password: {{ wavelog_password }}
|
|
|
|
All credentials saved to: ./mariadb_all_credentials.txt
|
|
Kubernetes secret updated: wavelog-db-secret |