Files
FabledSteward/steward/ansible/bundled/maintenance/docker_prune.yml
T
bvandeusen e8ac99174a
CI / lint (push) Successful in 2s
CI / unit (push) Successful in 44s
CI / integration (push) Successful in 2m36s
CI / publish (push) Successful in 1m11s
feat(ansible): parameterize docker_prune with prune_target (containers/images/system)
M78 step 1. The bundled maintenance/docker_prune.yml was a single
`docker system prune -f`; the upcoming per-host prune buttons need a
granular split. Add a `prune_target` extra-var:
- containers → docker container prune -f
- images     → docker image prune -f (+ -a when prune_all_images)
- system     → docker system prune -f (+ -a / --volumes) — the DEFAULT,
               so existing manual/scheduled callers (which set no var)
               keep today's behavior.

Reuses the existing bundled playbook rather than adding parallel files;
keeps the description/category/confirm meta so it still self-describes as
a confirm-gated maintenance run. Adds unit coverage: the playbook stays
valid YAML, its meta still discovers, and all three targets are guarded.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CAGR73DUowdVFVvYzLXC5C
2026-07-19 16:09:39 -04:00

65 lines
3.1 KiB
YAML

---
# description: Reclaim disk on Docker / Swarm nodes — prune stopped containers, unused images, or a full system prune.
# steward:category: maintenance
# steward:confirm: true
# Reclaim disk on Docker / Docker Swarm nodes by removing unused data.
# `prune_target` selects the scope (default `system` preserves the original
# behavior for existing manual/scheduled callers that don't set it):
# prune_target=containers remove stopped containers only (docker container prune)
# prune_target=images remove unused images (docker image prune;
# + prune_all_images=true → -a, i.e. ALL unused, not just dangling)
# prune_target=system docker system prune (dangling images, stopped
# containers, unused networks, build cache). Widen with:
# prune_all_images=true also ALL unused images
# prune_volumes=true also unused named volumes (data loss risk)
- name: Docker prune
hosts: all
gather_facts: false
become: true
vars:
prune_target: system
prune_all_images: false
prune_volumes: false
tasks:
- name: Validate prune_target
ansible.builtin.assert:
that: prune_target in ['containers', 'images', 'system']
fail_msg: "prune_target must be one of: containers, images, system (got '{{ prune_target }}')"
quiet: true
# ── Stopped containers only ───────────────────────────────────────────────
- name: Prune stopped containers
ansible.builtin.command:
argv: ['docker', 'container', 'prune', '-f']
register: container_prune
changed_when: "'Total reclaimed space: 0B' not in container_prune.stdout"
when: prune_target == 'containers'
# ── Unused images (dangling, or all unused with -a) ───────────────────────
- name: Prune unused images
ansible.builtin.command:
argv: >-
{{ ['docker', 'image', 'prune', '-f']
+ (['-a'] if prune_all_images | bool else []) }}
register: image_prune
changed_when: "'Total reclaimed space: 0B' not in image_prune.stdout"
when: prune_target == 'images'
# ── Full system prune (default) ───────────────────────────────────────────
- name: Run docker system prune
ansible.builtin.command:
argv: >-
{{ ['docker', 'system', 'prune', '-f']
+ (['-a'] if prune_all_images | bool else [])
+ (['--volumes'] if prune_volumes | bool else []) }}
register: system_prune
changed_when: "'Total reclaimed space: 0B' not in system_prune.stdout"
when: prune_target == 'system'
- name: Report reclaimed space
ansible.builtin.debug:
msg: >-
{{ ((container_prune.stdout_lines | default([]))
+ (image_prune.stdout_lines | default([]))
+ (system_prune.stdout_lines | default([]))) | select | list }}