236 lines
		
	
	
		
			7.9 KiB
		
	
	
	
		
			YAML
		
	
	
	
			
		
		
	
	
			236 lines
		
	
	
		
			7.9 KiB
		
	
	
	
		
			YAML
		
	
	
	
---
 | 
						|
- name: Docker Swarm Check 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
 | 
						|
      register: running_containers
 | 
						|
      
 | 
						|
    - name: Display running containers
 | 
						|
      debug:
 | 
						|
        msg: "🏃 Running Containers:\n{{ running_containers.stdout }}"
 | 
						|
        
 | 
						|
    # 获取所有容器(包括停止的)
 | 
						|
    - name: Get all containers
 | 
						|
      shell: docker ps -a
 | 
						|
      register: all_containers
 | 
						|
      
 | 
						|
    - name: Display all containers
 | 
						|
      debug:
 | 
						|
        msg: "📦 All Containers:\n{{ all_containers.stdout }}"
 | 
						|
        
 | 
						|
    # 获取容器名称列表
 | 
						|
    - name: Get container names
 | 
						|
      shell: docker ps -a | awk 'NR>1 {print $NF}' | head -20
 | 
						|
      register: container_names
 | 
						|
      
 | 
						|
    - name: Display container names
 | 
						|
      debug:
 | 
						|
        msg: "Container names: {{ container_names.stdout_lines }}"
 | 
						|
        
 | 
						|
    # 检查每个容器的基本信息
 | 
						|
    - name: Get basic container info
 | 
						|
      shell: |
 | 
						|
        echo "=== Container: {{ item }} ==="
 | 
						|
        docker inspect {{ item }} | jq -r '.[0] | {
 | 
						|
          "Image": .Config.Image,
 | 
						|
          "Status": .State.Status,
 | 
						|
          "RestartPolicy": .HostConfig.RestartPolicy.Name,
 | 
						|
          "NetworkMode": .HostConfig.NetworkMode
 | 
						|
        }'
 | 
						|
        echo "Ports:"
 | 
						|
        docker port {{ item }} 2>/dev/null || echo "No published ports"
 | 
						|
        echo "Mounts:"
 | 
						|
        docker inspect {{ item }} | jq -r '.[0].Mounts[]? | "  \(.Source):\(.Destination) (\(.Mode))"'
 | 
						|
        echo "---"        
 | 
						|
      register: container_info
 | 
						|
      loop: "{{ container_names.stdout_lines[:10] }}"  # 限制前10个容器
 | 
						|
      when: container_names.stdout_lines | length > 0
 | 
						|
      
 | 
						|
    - name: Display container info
 | 
						|
      debug:
 | 
						|
        msg: "{{ item.stdout }}"
 | 
						|
      loop: "{{ container_info.results }}"
 | 
						|
      when: container_info is defined
 | 
						|
      
 | 
						|
    # 检查 Docker Compose 文件
 | 
						|
    - name: Find docker-compose files in common locations
 | 
						|
      find:
 | 
						|
        paths: 
 | 
						|
          - /root
 | 
						|
          - /home
 | 
						|
          - /opt
 | 
						|
          - /var/lib/docker
 | 
						|
        patterns: 
 | 
						|
          - "docker-compose.yml"
 | 
						|
          - "docker-compose.yaml"
 | 
						|
          - "compose.yml"
 | 
						|
          - "compose.yaml"
 | 
						|
        recurse: yes
 | 
						|
        depth: 3
 | 
						|
      register: compose_files
 | 
						|
      ignore_errors: yes
 | 
						|
      
 | 
						|
    - name: Display found compose files
 | 
						|
      debug:
 | 
						|
        msg: "📄 Found compose files: {{ compose_files.files | map(attribute='path') | list }}"
 | 
						|
      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:\n{{ docker_networks.stdout }}"
 | 
						|
        
 | 
						|
    # 检查卷使用情况
 | 
						|
    - name: Get Docker volumes
 | 
						|
      shell: docker volume ls
 | 
						|
      register: docker_volumes
 | 
						|
      
 | 
						|
    - name: Display Docker volumes
 | 
						|
      debug:
 | 
						|
        msg: "💾 Docker Volumes:\n{{ docker_volumes.stdout }}"
 | 
						|
        
 | 
						|
    # 检查容器资源使用
 | 
						|
    - 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:\n{{ container_stats.stdout }}"
 | 
						|
      when: container_stats is defined
 | 
						|
      
 | 
						|
    # 检查 Docker 镜像
 | 
						|
    - name: Get Docker images
 | 
						|
      shell: docker images
 | 
						|
      register: docker_images
 | 
						|
      
 | 
						|
    - name: Display Docker images
 | 
						|
      debug:
 | 
						|
        msg: "🖼️ Docker Images:\n{{ docker_images.stdout }}"
 | 
						|
        
 | 
						|
    # 生成 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 if compose_files.files is defined else 0 }}
 | 
						|
          
 | 
						|
          💡 SWARM MIGRATION RECOMMENDATIONS:
 | 
						|
          
 | 
						|
          ✅ TYPICALLY SUITABLE FOR SWARM:
 | 
						|
          - Web servers (nginx, apache, caddy)
 | 
						|
          - API services and microservices
 | 
						|
          - Application servers
 | 
						|
          - Load balancers (traefik, haproxy)
 | 
						|
          - Monitoring tools (prometheus, grafana)
 | 
						|
          - Databases (with proper volume strategy)
 | 
						|
          
 | 
						|
          ❌ NOT SUITABLE FOR SWARM:
 | 
						|
          - Containers using Docker socket (/var/run/docker.sock)
 | 
						|
          - Containers with --privileged flag
 | 
						|
          - Development/testing containers
 | 
						|
          - Containers requiring specific host hardware access
 | 
						|
          
 | 
						|
          ⚠️  NEEDS MODIFICATION FOR SWARM:
 | 
						|
          - Containers using bind mounts → convert to volumes
 | 
						|
          - Containers without restart policies → add restart policies
 | 
						|
          - Containers using host networking → use overlay networks
 | 
						|
          - Containers with hardcoded IPs → use service discovery
 | 
						|
          
 | 
						|
          🚀 MIGRATION STEPS:
 | 
						|
          1. ✅ Swarm is already initialized
 | 
						|
          2. Create overlay networks for service communication
 | 
						|
          3. Convert suitable containers to Docker services
 | 
						|
          4. Set up service discovery and load balancing
 | 
						|
          5. Configure secrets and configs management
 | 
						|
          6. Test service scaling and failover
 | 
						|
          
 | 
						|
          📝 NEXT ACTIONS:
 | 
						|
          - Review each container above for Swarm suitability
 | 
						|
          - Identify services that would benefit from scaling
 | 
						|
          - Plan network topology for services
 | 
						|
          - Prepare volume migration strategy          
 | 
						|
      when: container_names is defined
 | 
						|
      
 | 
						|
    # 保存分析结果
 | 
						|
    - name: Save analysis summary to file
 | 
						|
      copy:
 | 
						|
        content: |
 | 
						|
          Docker Swarm Analysis for {{ inventory_hostname }}
 | 
						|
          Generated: {{ ansible_date_time.iso8601 }}
 | 
						|
          
 | 
						|
          SWARM STATUS:
 | 
						|
          {{ swarm_status.stdout }}
 | 
						|
          
 | 
						|
          CONTAINERS ({{ container_names.stdout_lines | length }} total):
 | 
						|
          {{ container_names.stdout_lines | join('\n') }}
 | 
						|
          
 | 
						|
          NETWORKS:
 | 
						|
          {{ docker_networks.stdout }}
 | 
						|
          
 | 
						|
          VOLUMES:
 | 
						|
          {{ docker_volumes.stdout }}
 | 
						|
          
 | 
						|
          IMAGES:
 | 
						|
          {{ docker_images.stdout }}
 | 
						|
          
 | 
						|
          {% if compose_files.files is defined and compose_files.files | length > 0 %}
 | 
						|
          COMPOSE FILES FOUND:
 | 
						|
          {% for file in compose_files.files %}
 | 
						|
          - {{ file.path }}
 | 
						|
          {% endfor %}
 | 
						|
          {% endif %}          
 | 
						|
        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 identify:
 | 
						|
          - Which services are suitable for Swarm
 | 
						|
          - Which containers need modification
 | 
						|
          - Migration priority and strategy
 | 
						|
          
 | 
						|
          💡 TIP: Focus on stateless services first for easier migration!           |