add database & users creation

This commit is contained in:
Ludovic Cartier
2025-09-24 18:41:06 +02:00
parent 278dba2c02
commit f816db7aad
5 changed files with 117 additions and 3 deletions

View File

@@ -7,6 +7,7 @@ An Ansible role for installing and configuring MariaDB on Debian-based systems u
- ✅ Official MariaDB repository setup
- ✅ Flexible version management (major and minor versions)
- ✅ Automatic upgrade capabilities
- ✅ Database and user management
- ✅ Idempotent operations
- ✅ Support for MariaDB versions 10, 11, and 12
- ✅ Proper service management during upgrades
@@ -29,6 +30,8 @@ None. The role works with sensible defaults.
| `mariadb_major_version` | `"12"` | Major version for repository setup (10, 11, or 12) |
| `mariadb_minor_version` | `undefined` | Minor version for specific version install (e.g., "8" for 11.8.x) |
| `mariadb_force_upgrade` | `false` | Force repository update and package upgrade |
| `mariadb_databases` | `[]` | List of databases to create |
| `mariadb_users` | `[]` | List of users to create |
### Variable Details
@@ -48,6 +51,24 @@ None. The role works with sensible defaults.
- **Purpose**: Forces repository reconfiguration and package upgrades
- **Use case**: Required when upgrading between major versions
#### `mariadb_databases`
- **Type**: List of dictionaries
- **Purpose**: Databases to create automatically
- **Structure**:
- `name` (required): Database name
- `encoding` (optional): Character encoding (default: `utf8mb4`)
- `collation` (optional): Collation (default: `utf8mb4_unicode_ci`)
#### `mariadb_users`
- **Type**: List of dictionaries
- **Purpose**: Users to create automatically
- **Structure**:
- `name` (required): Username
- `password` (optional): Plain text password
- `encrypted_password` (optional): Pre-encrypted password hash
- `host` (required): List of allowed hosts/IPs
- `priv` (optional): List of privileges
## Dependencies
None.
@@ -94,6 +115,38 @@ None.
mariadb_force_upgrade: true
```
### Complete Setup with Databases and Users
```yaml
- hosts: servers
roles:
- role: mariadb
vars:
mariadb_major_version: "12"
mariadb_databases:
- name: myapp_prod
encoding: utf8mb4
collation: utf8mb4_unicode_ci
- name: myapp_test
encoding: utf8
collation: utf8_general_ci
mariadb_users:
- name: app_user
password: "secure_password"
host:
- "localhost"
- "10.0.1.%"
priv:
- "myapp_prod.*:ALL"
- "myapp_test.*:ALL"
- name: backup_user
encrypted_password: "*8566479B619631314D83F27113F840A82191AB82"
host:
- "127.0.0.1"
priv:
- "*.*:SELECT,LOCK TABLES,SHOW VIEW,EVENT,TRIGGER"
```
## Usage Scenarios
### Fresh Installation
@@ -166,7 +219,8 @@ mariadb/
├── tasks/
│ ├── main.yml # Main task inclusion
│ ├── install.yml # Installation tasks
── upgrade.yml # Upgrade-specific tasks
── upgrade.yml # Upgrade-specific tasks
│ └── database.yml # Database and user management
└── handlers/
└── main.yml # Service handlers
```