infra/home/ansible/create-mariadb-container.yml
2025-09-18 16:08:26 -05:00

111 lines
No EOL
3.4 KiB
YAML

---
- name: Create MariaDB LXC Container
hosts: lab02
become: true
vars:
ct_id: 301
ct_name: mariadb
ct_template: "local:vztmpl/debian-13-standard_13.1-1_amd64.tar.zst"
ct_memory: 2048 # 2GB RAM
ct_disk_size: 50 # 50GB storage
ct_cores: 2
ct_ip: "10.0.19.6/22"
ct_gateway: "10.0.19.254"
ct_storage: "ceph" # Use Ceph RBD storage
mariadb_root_password: "dOtPgrVnkIW1zwfjvW9rSw"
tasks:
- name: Create MariaDB LXC container
command: |
pct create {{ ct_id }} {{ ct_template }} \
--hostname {{ ct_name }} \
--memory {{ ct_memory }} \
--cores {{ ct_cores }} \
--net0 name=eth0,bridge=vmbr0,ip={{ ct_ip }},gw={{ ct_gateway }} \
--storage {{ ct_storage }} \
--rootfs {{ ct_storage }}:{{ ct_disk_size }} \
--unprivileged 1 \
--features nesting=1 \
--onboot 1 \
--startup order=20
- name: Start container
command: pct start {{ ct_id }}
- name: Wait for container to boot
pause:
seconds: 30
- name: Update container packages
shell: pct exec {{ ct_id }} -- apt update && pct exec {{ ct_id }} -- apt upgrade -y
- name: Install MariaDB
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 }} -- bash -c "
systemctl enable mariadb
systemctl start mariadb
"
- name: Configure MariaDB for network access
shell: |
pct exec {{ ct_id }} -- bash -c "
# Update bind-address
sed -i 's/^bind-address.*=.*/bind-address = 0.0.0.0/' /etc/mysql/mariadb.conf.d/50-server.cnf
# Set root password and configure access
mysql -u root << 'EOF'
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('{{ mariadb_root_password }}');
CREATE USER IF NOT EXISTS 'root'@'%' IDENTIFIED BY '{{ mariadb_root_password }}';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF
# Restart MariaDB
systemctl restart mariadb
"
- name: Save credentials to file
copy:
content: |
MariaDB Container Credentials
=============================
Container ID: {{ ct_id }}
Host: lab02
IP Address: 10.0.19.6
Port: 3306
Root Credentials:
Username: root
Password: {{ mariadb_root_password }}
Connection String:
mysql -h 10.0.19.6 -u root -p'{{ mariadb_root_password }}'
Note: This container was created on {{ ansible_date_time.iso8601 }}
dest: "./mariadb_credentials.txt"
mode: '0600'
delegate_to: localhost
- name: Display connection info
debug:
msg: |
MariaDB container created successfully!
Container details:
- Container ID: {{ ct_id }}
- Host: lab02
- IP: 10.0.19.6
- Storage: {{ ct_disk_size }}GB on {{ ct_storage }}
Root credentials:
- Username: root
- Password: {{ mariadb_root_password }}
Credentials saved to: ./mariadb_credentials.txt