stop-delete-data.yaml
Overview
This YAML file defines a sequence of Ansible tasks aimed at performing a controlled shutdown of a service environment, followed by the cleanup of related Docker containers and removal of persistent data directories and configuration files. It is primarily used to stop running components gracefully and ensure that no residual data or configuration artifacts remain on the host system.
The tasks are executed in order to:
Trigger a graceful shutdown of nodes to speed up the process.
Stop and remove Docker containers and orphaned services related to the application.
Remove the persistent data directory to clear stored data.
Delete a specific configuration file (
bk_set.json) associated with the backup set.
Detailed Description of Tasks
1. Node graceful shutdown for speedup
- name: Node graceful shutdown for speedup
include_tasks: graceful-shutdown.yaml
vars:
GS_WAIT: no
Purpose:
This task includes an external task file (graceful-shutdown.yaml) that handles the orderly shutdown of nodes involved in the system. The variableGS_WAITis set to"no", indicating that the shutdown process should not wait for confirmation or delays, thus speeding up the shutdown.Parameters:
GS_WAIT: Controls the behavior of the shutdown task; here it disables waiting.
Usage:
This task is used to ensure that all nodes are stopped gracefully before proceeding with container teardown and data deletion, minimizing the risk of data corruption.Interaction:
It depends on thegraceful-shutdown.yamltask file, which likely contains the specific steps for signaling nodes to shut down.
2. Compose down
- name: Compose down
ansible.builtin.shell:
chdir: "{{ BK_DIR }}"
cmd: docker compose down --remove-orphans
ignore_errors: true
Purpose:
This task executes a shell command to stop and remove all Docker containers defined by a Docker Compose configuration located in the directory specified by the variableBK_DIR.Parameters:
chdir: Changes the directory toBK_DIRbefore running the command.cmd: The shell commanddocker compose down --remove-orphansstops containers, removes networks, volumes, and orphans.
Return Values:
The task does not explicitly return values, but its success indicates the environment has been stopped cleanly.Error Handling:
ignore_errors: trueensures that the playbook continues even if this step fails, allowing subsequent cleanup tasks to proceed.Usage:
This is used to stop Docker services and clean up orphan containers that might not be part of the current Compose file but were left running.
3. Remove data folder
- name: Remove data folder
ansible.builtin.file:
path: "{{ BK_DATA_DIR }}"
state: absent
Purpose:
Removes the directory specified byBK_DATA_DIR, which contains stored data related to the backup or application.Parameters:
path: Path to the data directory to be deleted.state: absent: Ensures the directory and all its contents are removed.
Usage:
This is a cleanup step to remove persistent data after the containers have been stopped, ensuring no leftover data remains.
4. Remove stored BK set
- name: Remove stored BK set
ansible.builtin.file:
path: "{{ BK_DIR }}/bk-configs/bk_set.json"
state: absent
Purpose:
Deletes the JSON configuration filebk_set.jsonlocated inside thebk-configsdirectory withinBK_DIR.Parameters:
path: Absolute path to the file to remove.state: absent: Indicates that the file should be deleted.
Usage:
This task clears any stored backup set configurations, effectively resetting the backup-related state.
Implementation Details and Algorithms
The file uses Ansible's declarative syntax to perform system administration tasks.
Variables such as
BK_DIRandBK_DATA_DIRare expected to be defined elsewhere in the inventory or playbook.The graceful shutdown is modularized by including a separate task file, promoting reuse and separation of concerns.
ignore_errors: trueon the Docker compose down step is a safeguard allowing the cleanup to proceed even if container shutdown encounters issues.The removal of directories and files uses Ansible's
filemodule withstate: absent, which recursively deletes directories or removes files as needed.
Interaction with Other Components
graceful-shutdown.yaml:
The included task file is responsible for the controlled shutdown of system nodes. The variableGS_WAITpassed to it affects its behavior.Docker Compose setup:
Thedocker compose downcommand interacts with Docker services defined in the directoryBK_DIR. This file assumes that Docker Compose configurations and running containers exist there.Persistent data and configurations:
The paths referenced (BK_DATA_DIR,BK_DIR/bk-configs/bk_set.json) connect with the system's persistent storage and configuration management, ensuring these resources are cleaned up during the deletion process.
Visual Diagram
flowchart TD
A[Start] --> B[Node graceful shutdown]
B --> C[Docker Compose down]
C --> D[Remove data folder]
D --> E[Remove bk_set.json]
E --> F[End]
subgraph GracefulShutdown
B
end
subgraph DockerCleanup
C
end
subgraph CleanupData
D --> E
end
The flowchart above represents the sequential workflow of the tasks defined in this YAML file.
Each block corresponds to a task, showing the order of execution from graceful shutdown to data and configuration cleanup.
For further details on Ansible task structures and modules used, reference Ansible Tasks and Modules. For information about Docker Compose command usage and options, see Docker Compose. For node shutdown procedures, refer to Node Management.