--- - name: Simple Docker Swarm Analysis for ash3c hosts: ash3c become: yes gather_facts: yes tasks: # 基础检查 - name: Check if Docker is installed command: which docker register: docker_installed failed_when: false changed_when: false - name: Fail if Docker not installed fail: msg: "Docker is not installed on {{ inventory_hostname }}" when: docker_installed.rc != 0 # 检查当前 Swarm 状态 - name: Check Docker Swarm status shell: docker info | grep "Swarm:" -A 1 register: swarm_status - name: Display current Swarm status debug: msg: "🔍 Current Swarm Status: {{ swarm_status.stdout_lines }}" # 获取运行中的容器 - name: Get running containers shell: docker ps --format "table {{ '{{' }}.Names{{ '}}' }}\t{{ '{{' }}.Image{{ '}}' }}\t{{ '{{' }}.Status{{ '}}' }}\t{{ '{{' }}.Ports{{ '}}' }}" register: running_containers - name: Display running containers debug: msg: "🏃 Running Containers: {{ running_containers.stdout_lines }}" # 获取所有容器(包括停止的) - name: Get all containers shell: docker ps -a --format "table {{ '{{' }}.Names{{ '}}' }}\t{{ '{{' }}.Image{{ '}}' }}\t{{ '{{' }}.Status{{ '}}' }}" register: all_containers - name: Display all containers debug: msg: "📦 All Containers: {{ all_containers.stdout_lines }}" # 检查每个容器的详细信息 - name: Get container names only shell: docker ps -a --format "{{ '{{' }}.Names{{ '}}' }}" register: container_names - name: Inspect each container shell: | echo "=== Container: {{ item }} ===" echo "Image: $(docker inspect {{ item }} --format '{{ '{{' }}.Config.Image{{ '}}' }}')" echo "Status: $(docker inspect {{ item }} --format '{{ '{{' }}.State.Status{{ '}}' }}')" echo "Restart Policy: $(docker inspect {{ item }} --format '{{ '{{' }}.HostConfig.RestartPolicy.Name{{ '}}' }}')" echo "Network Mode: $(docker inspect {{ item }} --format '{{ '{{' }}.HostConfig.NetworkMode{{ '}}' }}')" echo "Published Ports: $(docker port {{ item }} 2>/dev/null || echo 'None')" echo "Volumes/Mounts:" docker inspect {{ item }} --format '{{ '{{' }}range .Mounts{{ '}}' }} {{ '{{' }}.Source{{ '}}' }}:{{ '{{' }}.Destination{{ '}}' }} ({{ '{{' }}.Mode{{ '}}' }}){{ '{{' }}"\n"{{ '}}' }}{{ '{{' }}end{{ '}}' }}' || echo " None" echo "Environment Variables:" docker inspect {{ item }} --format '{{ '{{' }}range .Config.Env{{ '}}' }} {{ '{{' }}.{{ '}}' }}{{ '{{' }}"\n"{{ '}}' }}{{ '{{' }}end{{ '}}' }}' | head -10 echo "Labels:" docker inspect {{ item }} --format '{{ '{{' }}range $key, $value := .Config.Labels{{ '}}' }} {{ '{{' }}$key{{ '}}' }}={{ '{{' }}$value{{ '}}' }}{{ '{{' }}"\n"{{ '}}' }}{{ '{{' }}end{{ '}}' }}' | head -5 echo "---" register: container_inspect loop: "{{ container_names.stdout_lines }}" when: container_names.stdout_lines | length > 0 - name: Display container inspection results debug: msg: "{{ item.stdout }}" loop: "{{ container_inspect.results }}" when: container_inspect is defined # 检查 Docker Compose 文件 - name: Find docker-compose files find: paths: - /root - /home - /opt patterns: - "docker-compose.yml" - "docker-compose.yaml" - "compose.yml" - "compose.yaml" recurse: yes depth: 3 register: compose_files - name: Display found compose files debug: msg: "📄 Found compose files: {{ item.path }}" loop: "{{ compose_files.files }}" when: compose_files.files | length > 0 # 分析网络配置 - name: Get Docker networks shell: docker network ls register: docker_networks - name: Display Docker networks debug: msg: "🌐 Docker Networks: {{ docker_networks.stdout_lines }}" # 检查卷使用情况 - name: Get Docker volumes shell: docker volume ls register: docker_volumes - name: Display Docker volumes debug: msg: "💾 Docker Volumes: {{ docker_volumes.stdout_lines }}" # 检查容器资源使用 - name: Get container resource usage shell: docker stats --no-stream register: container_stats when: container_names.stdout_lines | length > 0 - name: Display container stats debug: msg: "📊 Container Resource Usage: {{ container_stats.stdout_lines }}" when: container_stats is defined # 生成 Swarm 适用性分析 - name: Generate Swarm suitability analysis debug: msg: | 🔍 DOCKER SWARM MIGRATION ANALYSIS FOR {{ inventory_hostname }} ================================================================ 📋 SUMMARY: - Current Swarm Status: {{ 'Active' if 'active' in swarm_status.stdout else 'Inactive' }} - Total Containers: {{ container_names.stdout_lines | length }} - Running Containers: {{ (running_containers.stdout_lines | length) - 1 }} - Compose Files Found: {{ compose_files.files | length }} 💡 GENERAL RECOMMENDATIONS: ✅ SUITABLE FOR SWARM (typically): - Web applications (nginx, apache, etc.) - API services - Databases (with proper volume management) - Monitoring tools (prometheus, grafana, etc.) - Load balancers ❌ NOT SUITABLE FOR SWARM: - Containers using Docker socket (/var/run/docker.sock) - Containers with --privileged flag - Containers requiring specific host access - Development/testing containers ⚠️ NEEDS MODIFICATION: - Containers using bind mounts (convert to volumes) - Containers without restart policies - Containers using host networking 🚀 NEXT STEPS: 1. Review each container's configuration above 2. Identify services that can benefit from scaling 3. Convert suitable containers to Docker services 4. Set up overlay networks 5. Configure secrets and configs management 📝 MIGRATION CHECKLIST: □ Initialize Swarm (already done: {{ 'Yes' if 'active' in swarm_status.stdout else 'No' }}) □ Create overlay networks □ Convert containers to services □ Set up service discovery □ Configure load balancing □ Test service scaling □ Set up monitoring when: container_names is defined # 保存分析结果 - name: Save analysis summary copy: content: | Docker Swarm Analysis for {{ inventory_hostname }} Generated: {{ ansible_date_time.iso8601 }} Current Swarm Status: {{ swarm_status.stdout }} Total Containers: {{ container_names.stdout_lines | length }} Container List: {{ container_names.stdout_lines | join('\n') }} Networks: {{ docker_networks.stdout }} Volumes: {{ docker_volumes.stdout }} Compose Files Found: {% for file in compose_files.files %} - {{ file.path }} {% endfor %} dest: "/tmp/swarm-analysis-{{ inventory_hostname }}-{{ ansible_date_time.epoch }}.txt" - name: Analysis complete debug: msg: | 🎉 Analysis complete! Results saved to: /tmp/swarm-analysis-{{ inventory_hostname }}-{{ ansible_date_time.epoch }}.txt Review the container details above to determine which services are suitable for Swarm migration.