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@<instance>.service, reset failed state - remove instance data dir, install dir, config file and run dir
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user