diff --git a/playbooks/serguzim.net.yml b/playbooks/serguzim.net.yml index c75d9c2..79927c7 100644 --- a/playbooks/serguzim.net.yml +++ b/playbooks/serguzim.net.yml @@ -30,3 +30,14 @@ tags: - caddy - never + + - name: Stop unused services + ansible.builtin.include_tasks: + file: tasks/stop-unused.yml + apply: + tags: + - cleanup + - always + tags: + - cleanup + - always diff --git a/playbooks/tasks/stop-unused.yml b/playbooks/tasks/stop-unused.yml new file mode 100644 index 0000000..0cd8b11 --- /dev/null +++ b/playbooks/tasks/stop-unused.yml @@ -0,0 +1,17 @@ +- name: Get running compose projects + ansible.builtin.shell: + cmd: docker container ls --format json | jq .Labels | grep -oe 'com.docker.compose.project=[a-zA-Z0-9_-]*' | sed -e 's/com.docker.compose.project=//' | sort | uniq + changed_when: false + register: docker_compose_projects_result + +- name: Get unused services + ansible.builtin.set_fact: + unused_services: "{{ docker_compose_projects_result.stdout_lines | difference(host_services) }}" + +- name: Stop unused services + ansible.builtin.command: + cmd: docker compose down + chdir: "{{ (services_path, item) | path_join }}" + changed_when: true + loop: "{{ unused_services }}" +