fix: improve service cleanup task to accurately check and stop disabled services
Some checks failed
Ansible Lint Check / check-ansible (push) Failing after 15s
Python Lint Check / check-python (push) Has been cancelled
Nix Format Check / check-format (push) Has been cancelled

This commit is contained in:
Menno van Leeuwen 2025-03-12 14:41:11 +01:00
parent 69158595ef
commit 0128bb4adf
Signed by: vleeuwenmenno
SSH Key Fingerprint: SHA256:OJFmjANpakwD3F2Rsws4GLtbdz1TJ5tkQF0RZmF0TRE

View File

@ -15,19 +15,26 @@
services_to_cleanup: "{{ services | selectattr('enabled', 'equalto', false) | list }}"
- name: Check running containers for disabled services
ansible.builtin.shell: set -o pipefail && docker ps --format '{% raw %}{{.Names}}{% endraw %}' | grep -q {{ item.name }}
ansible.builtin.shell: >
set -o pipefail &&
docker compose -f "{{ ansible_env.HOME }}/services/{{ item.name }}/docker-compose.yml" ps --format '{% raw %}{{.Name}}{% endraw %}' |
grep -q {{ item.name }} || true
register: container_check_results
loop: "{{ services_to_cleanup }}"
failed_when: false
changed_when: false
loop_control:
label: "{{ item.name }}"
when:
- item.name in (service_dir_results.results | map(attribute='item.name') | list)
- (service_dir_results.results | selectattr('item.name', 'equalto', item.name) | first).stat.exists
- name: Stop disabled services if running
ansible.builtin.command: docker compose -f "{{ ansible_env.HOME }}/services/{{ item.0.name }}/docker-compose.yml" down --remove-orphans
loop: "{{ services_to_cleanup | zip(container_check_results.results) | list }}"
ansible.builtin.command: docker compose -f "{{ ansible_env.HOME }}/services/{{ item.name }}/docker-compose.yml" down --remove-orphans
loop: "{{ services_to_cleanup }}"
when:
- item.1.rc is defined
- item.1.rc == 0
- item.name in (service_dir_results.results | map(attribute='item.name') | list)
- (service_dir_results.results | selectattr('item.name', 'equalto', item.name) | first).stat.exists
- (container_check_results.results | selectattr('item.name', 'equalto', item.name) | first).rc == 0
loop_control:
label: "{{ item.0.name }}"
label: "{{ item.name }}"