--- - name: Remove framework-production Stack from Production Server hosts: production become: no gather_facts: yes vars: stack_name: framework-production stack_path: "~/framework-production" tasks: - name: Check if Docker is running systemd: name: docker state: started register: docker_service become: yes - name: Fail if Docker is not running fail: msg: "Docker service is not running" when: docker_service.status.ActiveState != 'active' - name: Check if framework-production stack directory exists stat: path: "{{ stack_path }}" register: stack_dir - name: Check if framework-production containers exist (all states) shell: | docker ps -a --filter "name={{ stack_name }}" --format "{{ '{{' }}.Names{{ '}}' }}" args: executable: /bin/bash register: all_containers changed_when: false failed_when: false - name: Display all containers found debug: msg: "Found containers: {{ all_containers.stdout_lines if all_containers.stdout_lines | length > 0 else 'None' }}" - name: List all containers to find framework-production related ones shell: | docker ps -a --format "{{ '{{' }}.Names{{ '}}' }}\t{{ '{{' }}.Image{{ '}}' }}\t{{ '{{' }}.Status{{ '}}' }}" args: executable: /bin/bash register: all_containers_list changed_when: false failed_when: false - name: Display all containers debug: msg: "{{ all_containers_list.stdout_lines }}" - name: Check for containers with framework-production in name or image shell: | docker ps -a --format "{{ '{{' }}.Names{{ '}}' }}" | grep -iE "(framework-production|^db$|^php$|^web$)" || echo "" args: executable: /bin/bash register: matching_containers changed_when: false failed_when: false - name: Check for containers with framework-production images shell: | docker ps -a --format "{{ '{{' }}.Names{{ '}}' }}\t{{ '{{' }}.Image{{ '}}' }}" | grep -i "framework-production" | cut -f1 || echo "" args: executable: /bin/bash register: image_based_containers changed_when: false failed_when: false - name: Display found containers debug: msg: - "Containers by name pattern: {{ matching_containers.stdout_lines if matching_containers.stdout_lines | length > 0 else 'None' }}" - "Containers by image: {{ image_based_containers.stdout_lines if image_based_containers.stdout_lines | length > 0 else 'None' }}" - name: Stop and remove containers using docker-compose if stack directory exists shell: | cd {{ stack_path }} docker-compose down -v args: executable: /bin/bash when: stack_dir.stat.exists register: compose_down_result changed_when: true ignore_errors: yes - name: Stop and remove containers by name pattern and image shell: | REMOVED_CONTAINERS="" # Method 1: Remove containers with framework-production in image name while IFS=$'\t' read -r container image; do if [[ "$image" == *"framework-production"* ]]; then echo "Stopping and removing container '$container' (image: $image)" docker stop "$container" 2>/dev/null || true docker rm "$container" 2>/dev/null || true REMOVED_CONTAINERS="$REMOVED_CONTAINERS $container" fi done < <(docker ps -a --format "{{ '{{' }}.Names{{ '}}' }}\t{{ '{{' }}.Image{{ '}}' }}") # Method 2: Remove containers with specific names that match the pattern for container_name in "db" "php" "web"; do # Check if container exists and has framework-production image container_info=$(docker ps -a --filter "name=^${container_name}$" --format "{{ '{{' }}.Names{{ '}}' }}\t{{ '{{' }}.Image{{ '}}' }}" 2>/dev/null || echo "") if [[ -n "$container_info" ]]; then image=$(echo "$container_info" | cut -f2) if [[ "$image" == *"framework-production"* ]] || [[ "$image" == *"mariadb"* ]]; then echo "Stopping and removing container '$container_name' (image: $image)" docker stop "$container_name" 2>/dev/null || true docker rm "$container_name" 2>/dev/null || true REMOVED_CONTAINERS="$REMOVED_CONTAINERS $container_name" fi fi done # Method 3: Remove containers with framework-production in name docker ps -a --format "{{ '{{' }}.Names{{ '}}' }}" | grep -i framework-production | while read container; do if [ ! -z "$container" ]; then echo "Stopping and removing container '$container'" docker stop "$container" 2>/dev/null || true docker rm "$container" 2>/dev/null || true REMOVED_CONTAINERS="$REMOVED_CONTAINERS $container" fi done # Output removed containers if [[ -n "$REMOVED_CONTAINERS" ]]; then echo "Removed containers:$REMOVED_CONTAINERS" else echo "No containers were removed" fi args: executable: /bin/bash register: direct_remove_result changed_when: "'Removed containers' in direct_remove_result.stdout" failed_when: false - name: Remove stack directory if it exists file: path: "{{ stack_path }}" state: absent when: stack_dir.stat.exists register: dir_removed - name: Verify all framework-production containers are removed shell: | docker ps -a --format "{{ '{{' }}.Names{{ '}}' }}" | grep -i framework-production || echo "" args: executable: /bin/bash register: remaining_containers changed_when: false failed_when: false - name: Display removal status debug: msg: - "=== framework-production Stack Removal Complete ===" - "Stack directory removed: {{ 'Yes' if dir_removed.changed else 'No (did not exist)' }}" - "Containers removed: {{ 'Yes' if (compose_down_result.changed or direct_remove_result.changed) else 'No (none found)' }}" - "Remaining containers: {{ remaining_containers.stdout if remaining_containers.stdout else 'None' }}" - "" - "Stack '{{ stack_name }}' has been successfully removed."