From b9a3ddd9e351292aa86b72ceb78e071033385859 Mon Sep 17 00:00:00 2001 From: Hungerdream <1710233908@qq.com> Date: Tue, 28 Jul 2026 14:28:42 +0800 Subject: [PATCH] feat(ansible): add mysql-rollback playbook for instance cleanup Add a compensating playbook that removes a single delivered MySQL instance from a target host. The rollback scope is pinned by target_hosts + instance_name + data_disk, with pre-task assertions rejecting instance names or disk paths outside the delivery layout to prevent accidental deletion. - stop and disable mysql-delivery@.service, reset failed state - remove instance data dir, install dir, config file and run dir --- ansible/mysql-rollback.yml | 79 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 ansible/mysql-rollback.yml diff --git a/ansible/mysql-rollback.yml b/ansible/mysql-rollback.yml new file mode 100644 index 0000000..c302b1d --- /dev/null +++ b/ansible/mysql-rollback.yml @@ -0,0 +1,79 @@ +--- +- name: Roll back native MySQL delivery instance + hosts: "{{ target_hosts }}" + become: true + gather_facts: false + any_errors_fatal: true + vars: + mysql_instance: "{{ instance_name }}" + mysql_data_disk: "{{ data_disk }}" + mysql_base_dir: "{{ mysql_data_disk }}/mysql-delivery/{{ mysql_instance }}" + mysql_install_dir: "/opt/mysql-delivery/{{ mysql_instance }}" + mysql_config_file: "/etc/mysql/mysql-delivery/{{ mysql_instance }}.cnf" + mysql_run_dir: "/run/mysql-delivery-{{ mysql_instance }}" + + pre_tasks: + - name: Validate rollback target + ansible.builtin.assert: + that: + - mysql_instance is match('^[a-z0-9][a-z0-9-]{0,62}$') + - mysql_data_disk is match('^/') + - mysql_data_disk != '/' + - "'..' not in mysql_data_disk" + fail_msg: "Rollback target is outside the native MySQL delivery layout" + quiet: true + + tasks: + - name: Stop the delivery instance when present + ansible.builtin.systemd_service: + name: "mysql-delivery@{{ mysql_instance }}.service" + state: stopped + enabled: false + register: mysql_stop_result + failed_when: false + + - name: Remove the instance systemd failure state + ansible.builtin.command: + argv: + - systemctl + - reset-failed + - "mysql-delivery@{{ mysql_instance }}.service" + changed_when: false + failed_when: false + + - name: Remove the instance configuration + ansible.builtin.file: + path: "{{ mysql_config_file }}" + state: absent + + - name: Remove instance-local binary links + ansible.builtin.file: + path: "{{ mysql_install_dir }}" + state: absent + + - name: Remove the instance data and log tree + ansible.builtin.file: + path: "{{ mysql_base_dir }}" + state: absent + + - name: Remove the instance runtime directory + ansible.builtin.file: + path: "{{ mysql_run_dir }}" + state: absent + + - name: Confirm the instance artifacts are gone + ansible.builtin.stat: + path: "{{ item }}" + loop: + - "{{ mysql_config_file }}" + - "{{ mysql_install_dir }}" + - "{{ mysql_base_dir }}" + - "{{ mysql_run_dir }}" + register: mysql_rollback_artifacts + + - name: Assert that all instance artifacts are gone + ansible.builtin.assert: + that: + - mysql_rollback_artifacts.results | selectattr('stat.exists') | list | length == 0 + fail_msg: "One or more MySQL delivery artifacts remain after rollback" + quiet: true