Files
xinfra/ansible/postgresql-rollback.yml

87 lines
3.4 KiB
YAML

---
- name: Roll back native PostgreSQL delivery instances
hosts: "{{ target_hosts }}"
become: true
gather_facts: false
any_errors_fatal: true
vars:
postgresql_instance_plan: "{{ postgresql_instances[inventory_hostname] | default({}) }}"
postgresql_instance_id: "{{ postgresql_instance_plan.instance_id | default('') }}"
postgresql_data_root: /data/postgresql
postgresql_instance_root: "{{ postgresql_data_root }}/{{ postgresql_instance_id }}"
postgresql_data_dir: "{{ postgresql_instance_plan.data_dir | default('') }}"
postgresql_config_dir: "{{ postgresql_instance_plan.config_dir | default('') }}"
postgresql_log_dir: "{{ postgresql_instance_plan.log_dir | default('') }}"
postgresql_systemd_unit: "{{ postgresql_instance_plan.systemd_unit | default('') }}"
postgresql_dropin_dir: "/etc/systemd/system/{{ postgresql_systemd_unit }}.d"
postgresql_run_dir: "/run/postgresql-xinfra/{{ postgresql_instance_id }}"
pre_tasks:
- name: Validate PostgreSQL rollback target
ansible.builtin.assert:
that:
- postgresql_instance_plan is mapping
- postgresql_instance_id is match('^[a-z0-9][a-z0-9-]{0,62}$')
- postgresql_instance_root == '/data/postgresql/' + postgresql_instance_id
- postgresql_data_dir == postgresql_instance_root + '/data'
- postgresql_config_dir == postgresql_instance_root + '/conf'
- postgresql_log_dir == postgresql_instance_root + '/log'
- postgresql_systemd_unit == 'postgresql-xinfra@' + postgresql_instance_id + '.service'
- postgresql_instance_root != '/data/postgresql'
- "'..' not in postgresql_instance_root"
fail_msg: "Rollback target is outside the native PostgreSQL delivery layout"
quiet: true
tasks:
- name: Stop the PostgreSQL delivery instance when present
ansible.builtin.systemd_service:
name: "{{ postgresql_systemd_unit }}"
state: stopped
enabled: false
failed_when: false
- name: Reset the PostgreSQL instance failure state
ansible.builtin.command:
argv:
- systemctl
- reset-failed
- "{{ postgresql_systemd_unit }}"
changed_when: false
failed_when: false
- name: Remove the PostgreSQL instance systemd drop-in
ansible.builtin.file:
path: "{{ postgresql_dropin_dir }}"
state: absent
- name: Remove the PostgreSQL instance data, configuration, and logs
ansible.builtin.file:
path: "{{ postgresql_instance_root }}"
state: absent
- name: Remove the PostgreSQL instance runtime directory
ansible.builtin.file:
path: "{{ postgresql_run_dir }}"
state: absent
- name: Reload systemd after removing the instance drop-in
ansible.builtin.systemd_service:
daemon_reload: true
- name: Confirm PostgreSQL instance artifacts are gone
ansible.builtin.stat:
path: "{{ item }}"
loop:
- "{{ postgresql_dropin_dir }}"
- "{{ postgresql_instance_root }}"
- "{{ postgresql_run_dir }}"
register: postgresql_rollback_artifacts
- name: Assert all PostgreSQL instance artifacts are gone
ansible.builtin.assert:
that:
- not item.stat.exists
fail_msg: "PostgreSQL rollback left an instance artifact on the target host"
quiet: true
loop: "{{ postgresql_rollback_artifacts.results }}"