diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..600d2d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..856f89e --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2025 ansible-roles + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index e69de29..bc536b6 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,47 @@ +# Ansible Role: Percona Server + +Installs and manages Percona Server on Debian. + +## Requirements + +None. + +## Role Variables + +Available variables are listed below, along with default values (see `defaults/main.yml`): + +```yaml +percona_version: "8.4" +percona_root_password: "root_password_change_me" +percona_bind_address: "127.0.0.1" +percona_port: 3306 +percona_databases: [] +percona_users: [] +``` + +## Dependencies + +None. + +## Example Playbook + +```yaml +- hosts: database + roles: + - role: percona + vars: + percona_version: "8.4" + percona_root_password: "secure_password" + percona_databases: + - name: app_db + collation: utf8mb4_general_ci + encoding: utf8mb4 + percona_users: + - name: app_user + password: "app_password" + priv: "app_db.*:ALL" +``` + +## License + +MIT diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..ebf9b1f --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,33 @@ +--- +# Percona Server version. Supported values: "8.0", "8.4" +percona_version: "8.4" + +# Percona release setup package URL +percona_release_package_url: "https://repo.percona.com/apt/percona-release_latest.generic_all.deb" + +# MySQL root password +percona_root_password: "root_password_change_me" + +# Bind address +percona_bind_address: "127.0.0.1" + +# Port +percona_port: 3306 + +# Databases to create +# Example: +# percona_databases: +# - name: my_db +# collation: utf8mb4_general_ci +# encoding: utf8mb4 +percona_databases: [] + +# Users to create +# Example: +# percona_users: +# - name: my_user +# password: my_password +# host: "%" +# priv: "my_db.*:ALL" +percona_users: [] + diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..df7273e --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,5 @@ +--- +- name: Restart Percona Server + service: + name: mysql + state: restarted diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..2208662 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,18 @@ +--- +galaxy_info: + author: Ludovic Cartier + description: install & configure Percona Server + company: brainsys + license: MIT + min_ansible_version: "2.9" + issue_tracker_url: https://git.brainsys.io/ansible-roles/percona/issues + github_branch: main + platforms: + - name: Debian + versions: + - all + galaxy_tags: + - database + - percona + - mysql +dependencies: [] \ No newline at end of file diff --git a/tasks/configure.yml b/tasks/configure.yml new file mode 100644 index 0000000..7fb7d41 --- /dev/null +++ b/tasks/configure.yml @@ -0,0 +1,22 @@ +--- +- name: percona | configure Percona Server + template: + src: my.cnf.j2 + dest: /etc/mysql/conf.d/01-ansible.cnf + mode: '0644' + notify: Restart Percona Server + +- name: percona | ensure Percona Server is started and enabled + service: + name: mysql + state: started + enabled: yes + +- name: percona | update MySQL root password + mysql_user: + name: root + host: localhost + password: "{{ percona_root_password }}" + login_unix_socket: /var/run/mysqld/mysqld.sock + priv: "*.*:ALL,GRANT" + ignore_errors: true # In case password is already set and socket auth is disabled diff --git a/tasks/databases.yml b/tasks/databases.yml new file mode 100644 index 0000000..911ee23 --- /dev/null +++ b/tasks/databases.yml @@ -0,0 +1,10 @@ +--- +- name: percona | create databases + mysql_db: + name: "{{ item.name }}" + collation: "{{ item.collation | default(omit) }}" + encoding: "{{ item.encoding | default(omit) }}" + state: present + login_user: root + login_password: "{{ percona_root_password }}" + loop: "{{ percona_databases }}" diff --git a/tasks/install.yml b/tasks/install.yml new file mode 100644 index 0000000..00de4f6 --- /dev/null +++ b/tasks/install.yml @@ -0,0 +1,27 @@ +--- +- name: percona | download Percona release package + get_url: + url: "{{ percona_release_package_url }}" + dest: /tmp/percona-release_latest.generic_all.deb + mode: '0644' + +- name: percona | install Percona release package + apt: + deb: /tmp/percona-release_latest.generic_all.deb + state: present + +- name: percona | determine Percona release string + set_fact: + percona_release_string: >- + {% if percona_version == '8.4' %}ps-84-lts{% elif percona_version == '8.0' %}ps-80{% else %}ps-80{% endif %} + +- name: percona | enable Percona repository + command: "percona-release setup -y {{ percona_release_string }}" + args: + creates: "/etc/apt/sources.list.d/percona-{{ percona_release_string }}-release.list" + +- name: percona | install Percona Server + apt: + name: percona-server-server + state: present + update_cache: yes diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..858a968 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,16 @@ +--- +- name: percona | requirements + include_tasks: requirements.yml + +- name: percona | installation + include_tasks: install.yml + +- name: percona | configuration + include_tasks: configure.yml + +- name: percona | databases + include_tasks: databases.yml + +- name: percona | users + include_tasks: users.yml + diff --git a/tasks/requirements.yml b/tasks/requirements.yml new file mode 100644 index 0000000..cd02861 --- /dev/null +++ b/tasks/requirements.yml @@ -0,0 +1,20 @@ +--- +- name: percona | apt update cache + ansible.builtin.apt: + update_cache: yes + cache_valid_time: 86400 + +- name: percona | install requirements + ansible.builtin.apt: + name: + - apt-transport-https + - lsb-release + - ca-certificates + - curl + - gnupg2 + state: present + +- name: percona | install python3-pymysql for database management + ansible.builtin.apt: + name: python3-pymysql + state: present diff --git a/tasks/users.yml b/tasks/users.yml new file mode 100644 index 0000000..6f9f83e --- /dev/null +++ b/tasks/users.yml @@ -0,0 +1,11 @@ +--- +- name: percona | create users + mysql_user: + name: "{{ item.name }}" + password: "{{ item.password }}" + host: "{{ item.host | default('%') }}" + priv: "{{ item.priv | default('*.*:USAGE') }}" + state: present + login_user: root + login_password: "{{ percona_root_password }}" + loop: "{{ percona_users }}" diff --git a/templates/my.cnf.j2 b/templates/my.cnf.j2 new file mode 100644 index 0000000..297c125 --- /dev/null +++ b/templates/my.cnf.j2 @@ -0,0 +1,9 @@ +[mysqld] +bind-address = {{ percona_bind_address }} +port = {{ percona_port }} + +# Character set and collation +character-set-server = utf8mb4 +collation-server = utf8mb4_general_ci + +# Other settings can be added here