From f7c3a8f2e5c25af6346edc9a4a59a20f5f58e4e5 Mon Sep 17 00:00:00 2001 From: Faustin Lammler Date: Tue, 26 Sep 2023 17:20:16 +0200 Subject: [PATCH] Make the role compatible with Alpine linux Should be compatible with: - 3.17 - 3.18 --- .github/workflows/test_cluster.yml | 1 + .github/workflows/test_default.yml | 2 ++ README.md | 4 +++- handlers/main.yml | 3 +++ meta/main.yml | 14 +++++------ molecule/cluster/verify.yml | 20 +++++++++------- molecule/default/vars/Alpine.yml | 1 + molecule/default/verify.yml | 38 ++++++++++++++++++------------ requirements.yml | 1 + tasks/main.yml | 4 ++++ tasks/setup_alpine.yml | 34 ++++++++++++++++++++++++++ tasks/setup_debian.yml | 4 ++-- tasks/setup_redhat.yml | 4 ++-- vars/Alpine.yml | 10 ++++++++ vars/Debian.yml | 1 + vars/RedHat.yml | 1 + 16 files changed, 106 insertions(+), 36 deletions(-) create mode 120000 molecule/default/vars/Alpine.yml create mode 100644 tasks/setup_alpine.yml create mode 100644 vars/Alpine.yml diff --git a/.github/workflows/test_cluster.yml b/.github/workflows/test_cluster.yml index b66c3dc..8db6b71 100644 --- a/.github/workflows/test_cluster.yml +++ b/.github/workflows/test_cluster.yml @@ -41,6 +41,7 @@ jobs: - fedora-38 - almalinux-9 - rockylinux-8 + - alpine-3.18 steps: - uses: actions/checkout@v4 - name: Install requirements diff --git a/.github/workflows/test_default.yml b/.github/workflows/test_default.yml index 99abc5e..e22d730 100644 --- a/.github/workflows/test_default.yml +++ b/.github/workflows/test_default.yml @@ -47,6 +47,8 @@ jobs: - almalinux-9 - rockylinux-8 - rockylinux-9 + - alpine-3.17 + - alpine-3.18 steps: - uses: actions/checkout@v4 - name: Install requirements diff --git a/README.md b/README.md index 4ec99c3..47eec5a 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![Cluster](https://github.com/fauust/ansible-role-mariadb/actions/workflows/test_cluster.yml/badge.svg)](https://github.com/fauust/ansible-role-mariadb/actions/workflows/test_cluster.yml) [![Cluster MDBF](https://github.com/fauust/ansible-role-mariadb/actions/workflows/test_cluster_mdbf.yml/badge.svg)](https://github.com/fauust/ansible-role-mariadb/actions/workflows/test_cluster_mdbf.yml) -Install and configure MariaDB Server on Linux (Debian or RHEL based). +Install and configure MariaDB Server on Linux (Debian, RHEL or Alpine linux based). Optionally, this role also permits one to: @@ -128,6 +128,8 @@ deployments and you are encouraged to use your own values. [`vars`](./vars). ```yaml +mariadb_user: "default value depends on OS" +mariadb_package: "default value depends on OS" mariadb_config_file: "default value depends on OS" mariadb_data_dir: "default value depends on OS" mariadb_port: 3306 diff --git a/handlers/main.yml b/handlers/main.yml index 08528fe..98f415f 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -6,3 +6,6 @@ when: - mariadb_can_restart - not ansible_check_mode + +- name: Setup MariaDB + ansible.builtin.command: /etc/init.d/mariadb setup diff --git a/meta/main.yml b/meta/main.yml index 464abbf..933df43 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -11,21 +11,19 @@ galaxy_info: platforms: - name: Debian versions: - - buster - - bullseye - - sid + - all - name: Ubuntu versions: - - bionic - - focal - - jammy + - all - name: Fedora versions: - all - name: EL versions: - - 8 - - 9 + - all + - name: Alpine + versions: + - all galaxy_tags: - mariadb - mysql diff --git a/molecule/cluster/verify.yml b/molecule/cluster/verify.yml index fe6c3b4..249a2a7 100644 --- a/molecule/cluster/verify.yml +++ b/molecule/cluster/verify.yml @@ -3,15 +3,19 @@ hosts: all tasks: - - name: Get mariadb service status - ansible.builtin.systemd: - name: "mariadb" - register: mariadb_service + - name: Make sure that service is started + ansible.builtin.service: + name: mariadb + state: started + register: status + failed_when: status.changed - - name: Check that mariadb service is active - ansible.builtin.assert: - that: - - mariadb_service.status.ActiveState == 'active' + - name: Make sure that service is enabled + ansible.builtin.service: + name: mariadb + state: enabled + register: status + failed_when: status.changed - name: Verify replication hosts: replica diff --git a/molecule/default/vars/Alpine.yml b/molecule/default/vars/Alpine.yml new file mode 120000 index 0000000..1a1229f --- /dev/null +++ b/molecule/default/vars/Alpine.yml @@ -0,0 +1 @@ +../../../vars/Alpine.yml \ No newline at end of file diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml index 55e9bb5..d92f35a 100644 --- a/molecule/default/verify.yml +++ b/molecule/default/verify.yml @@ -14,13 +14,17 @@ paths: - "vars" - - name: Gather package facts - ansible.builtin.package_facts: - manager: auto - - - name: Verify Packages + - name: Verify pkg status + ansible.builtin.package: + name: "{{ mariadb_package }}" + state: present + check_mode: true + register: pkg_status + + - name: Check that {{ mariadb_package }} already installed ansible.builtin.assert: - that: "'mariadb-server' in ansible_facts.packages|lower" + that: not pkg_status.changed + fail_msg: "{{ mariadb_package }} is not installed" - name: Register {{ mariadb_config_file }} ansible.builtin.stat: @@ -58,15 +62,19 @@ - logdir.stat.pw_name == "{{ mariadb_user }}" - logdir.stat.gr_name == "{{ mariadb_user }}" - - name: Get mariadb service status - ansible.builtin.systemd: - name: "mariadb" - register: mariadb_service - - - name: Check that mariadb service is active - ansible.builtin.assert: - that: - - mariadb_service.status.ActiveState == 'active' + - name: Make sure that service is started + ansible.builtin.service: + name: mariadb + state: started + register: status + failed_when: status.changed + + - name: Make sure that service is enabled + ansible.builtin.service: + name: mariadb + state: enabled + register: status + failed_when: status.changed - name: Check that 127.0.0.1:3306 is listening ansible.builtin.wait_for: diff --git a/requirements.yml b/requirements.yml index 802aa21..fee9312 100644 --- a/requirements.yml +++ b/requirements.yml @@ -1,4 +1,5 @@ --- collections: + - name: community.general - name: community.mysql - name: containers.podman diff --git a/tasks/main.yml b/tasks/main.yml index 4bbd339..428b4f5 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -17,6 +17,10 @@ ansible.builtin.import_tasks: setup_redhat.yml when: ansible_os_family == "RedHat" +- name: Include task setup_alpine.yml + ansible.builtin.import_tasks: setup_alpine.yml + when: ansible_os_family == "Alpine" + - name: Include task configure.yml ansible.builtin.import_tasks: configure.yml diff --git a/tasks/setup_alpine.yml b/tasks/setup_alpine.yml new file mode 100644 index 0000000..f5cf2d0 --- /dev/null +++ b/tasks/setup_alpine.yml @@ -0,0 +1,34 @@ +--- +- name: Install packages (package) + ansible.builtin.package: + name: + - "{{ mariadb_package }}" + - mariadb-client + - py3-pymysql + state: present + notify: Setup MariaDB + +- name: Check if mariadb command exists + ansible.builtin.stat: + path: /usr/bin/mariadb + register: mariadb_cmd + +- name: Make sure that mariadb command exists + ansible.builtin.file: + src: /usr/bin/mysql + dest: /usr/bin/mariadb + owner: root + group: root + state: link + when: mariadb_cmd.stat.exists is false + +- name: Ensure /run/mysqld + ansible.builtin.file: + path: /run/mysqld + state: directory + owner: mysql + group: mysql + mode: 0755 + +- name: Immediately setup MariaDB (necessary for configuration) + ansible.builtin.meta: flush_handlers diff --git a/tasks/setup_debian.yml b/tasks/setup_debian.yml index bf3822d..ca3f744 100644 --- a/tasks/setup_debian.yml +++ b/tasks/setup_debian.yml @@ -75,10 +75,10 @@ group: root mode: "0644" -- name: Install packages +- name: Install packages (apt) ansible.builtin.apt: package: - - mariadb-server + - "{{ mariadb_package }}" - python3-pymysql state: present update_cache: true diff --git a/tasks/setup_redhat.yml b/tasks/setup_redhat.yml index f2058a9..46b801c 100644 --- a/tasks/setup_redhat.yml +++ b/tasks/setup_redhat.yml @@ -20,10 +20,10 @@ mode: "0644" when: mariadb_use_official_repo -- name: Install packages +- name: Install packages (dnf) ansible.builtin.dnf: name: - - mariadb-server + - "{{ mariadb_package }}" - python3-PyMySQL state: present diff --git a/vars/Alpine.yml b/vars/Alpine.yml new file mode 100644 index 0000000..efc7050 --- /dev/null +++ b/vars/Alpine.yml @@ -0,0 +1,10 @@ +--- +mariadb_user: mysql +mariadb_package: mariadb +mariadb_config_file: "/etc/my.cnf" +mariadb_data_dir: "/var/lib/mysql" +mariadb_pid_file: "/run/mysqld/mariadb.pid" +mariadb_unix_socket: "/run/mysqld/mysqld.sock" +mariadb_log_dir: "/var/log/mariadb" +mariadb_log_error_file: "{{ mariadb_log_dir }}/error.log" +mariadb_cron_package_name: "cronie" diff --git a/vars/Debian.yml b/vars/Debian.yml index be01919..2ac21f7 100644 --- a/vars/Debian.yml +++ b/vars/Debian.yml @@ -1,5 +1,6 @@ --- mariadb_user: mysql +mariadb_package: mariadb-server mariadb_config_file: "/etc/mysql/mariadb.cnf" mariadb_data_dir: "/var/lib/mysql" mariadb_pid_file: "/run/mysqld/mysqld.pid" diff --git a/vars/RedHat.yml b/vars/RedHat.yml index 9c89f09..5ffce8b 100644 --- a/vars/RedHat.yml +++ b/vars/RedHat.yml @@ -1,5 +1,6 @@ --- mariadb_user: mysql +mariadb_package: mariadb-server mariadb_config_file: "/etc/my.cnf.d/mariadb-server.cnf" mariadb_data_dir: "/var/lib/mysql" mariadb_pid_file: "/run/mysqld.pid"