127 lines
4.4 KiB
YAML
127 lines
4.4 KiB
YAML
---
|
|
# Removes only this project's resources, matched by name. It never calls
|
|
# `podman system prune`, so unrelated containers, images and volumes on the
|
|
# host are left untouched.
|
|
- name: Remove the Star Citizen Inventory installation and its data
|
|
hosts: webservers
|
|
|
|
vars:
|
|
confirm_destroy: false
|
|
container_pattern: '^(sc-inventory|sc-inv-verify|trade-hub[-_])'
|
|
image_pattern: '^(localhost/)?(sc-inventory-|sc-inv-verify-|trade-hub[-_])'
|
|
volume_pattern: '^(sc-inventory-data|pgdata|trade-hub_pgdata)$'
|
|
network_pattern: '^(sc-inventory$|sc-inv-verify$)'
|
|
|
|
tasks:
|
|
- name: Require explicit confirmation for a destructive run
|
|
ansible.builtin.assert:
|
|
that:
|
|
- confirm_destroy | bool
|
|
fail_msg: >-
|
|
Refusing to run. This deletes the containers, images, network and the
|
|
PostgreSQL volume that holds every account and inventory. Re-run with:
|
|
ansible-playbook teardown.yml -e confirm_destroy=true
|
|
quiet: true
|
|
when: not ansible_check_mode
|
|
|
|
- name: Check whether podman is installed
|
|
ansible.builtin.command: podman --version
|
|
register: podman_check
|
|
failed_when: false
|
|
changed_when: false
|
|
check_mode: false
|
|
|
|
- name: End play when podman is absent
|
|
ansible.builtin.meta: end_play
|
|
when: podman_check.rc != 0
|
|
|
|
- name: List containers
|
|
ansible.builtin.command: podman ps -a --format json
|
|
register: existing_containers
|
|
changed_when: false
|
|
check_mode: false
|
|
|
|
- name: Remove project containers
|
|
containers.podman.podman_container:
|
|
name: "{{ item }}"
|
|
state: absent
|
|
loop: >-
|
|
{{ (existing_containers.stdout | from_json)
|
|
| map(attribute='Names')
|
|
| select('iterable')
|
|
| flatten
|
|
| unique
|
|
| select('match', container_pattern)
|
|
| list }}
|
|
|
|
- name: List images
|
|
ansible.builtin.command: podman images --format json
|
|
register: existing_images
|
|
changed_when: false
|
|
check_mode: false
|
|
|
|
- name: Remove project images (every tag, including stale builds)
|
|
ansible.builtin.command:
|
|
argv:
|
|
- podman
|
|
- rmi
|
|
- --force
|
|
- "{{ item }}"
|
|
loop: >-
|
|
{{ (existing_images.stdout | from_json)
|
|
| selectattr('Names', 'defined')
|
|
| selectattr('Names', 'iterable')
|
|
| map(attribute='Names')
|
|
| flatten
|
|
| unique
|
|
| select('match', image_pattern)
|
|
| list }}
|
|
failed_when: false
|
|
|
|
- name: List volumes
|
|
ansible.builtin.command: podman volume ls --format json
|
|
register: existing_volumes
|
|
changed_when: false
|
|
check_mode: false
|
|
|
|
- name: Remove the database volume (all accounts and inventories)
|
|
containers.podman.podman_volume:
|
|
name: "{{ item }}"
|
|
state: absent
|
|
loop: >-
|
|
{{ (existing_volumes.stdout | from_json)
|
|
| map(attribute='Name')
|
|
| unique
|
|
| select('match', volume_pattern)
|
|
| list }}
|
|
|
|
- name: List networks
|
|
ansible.builtin.command: podman network ls --format json
|
|
register: existing_networks
|
|
changed_when: false
|
|
check_mode: false
|
|
|
|
- name: Remove project networks
|
|
containers.podman.podman_network:
|
|
name: "{{ item }}"
|
|
state: absent
|
|
loop: >-
|
|
{{ (existing_networks.stdout | from_json)
|
|
| map(attribute='name')
|
|
| unique
|
|
| select('match', network_pattern)
|
|
| list }}
|
|
|
|
- name: Verify nothing from this project remains
|
|
ansible.builtin.shell:
|
|
cmd: |
|
|
set -eu
|
|
c=$(podman ps -a --format '{% raw %}{{ .Names }}{% endraw %}' | grep -Ec '^(sc-inventory|sc-inv-verify|trade-hub[-_])' || true)
|
|
i=$(podman images --format '{% raw %}{{ .Names }}{% endraw %}' | grep -Ec '^(localhost/)?(sc-inventory-|sc-inv-verify-|trade-hub[-_])' || true)
|
|
v=$(podman volume ls --format '{% raw %}{{ .Name }}{% endraw %}' | grep -Ec '^(sc-inventory-data|pgdata|trade-hub_pgdata)$' || true)
|
|
n=$(podman network ls --format '{% raw %}{{ .Name }}{% endraw %}' | grep -Ec '^(sc-inventory|sc-inv-verify)$' || true)
|
|
echo "remaining: containers=$c images=$i volumes=$v networks=$n"
|
|
[ "$c" -eq 0 ] && [ "$i" -eq 0 ] && [ "$v" -eq 0 ] && [ "$n" -eq 0 ]
|
|
changed_when: false
|
|
when: not ansible_check_mode
|