---
- name: Kali Linux 安全工具测试
  hosts: kali
  become: yes
  gather_facts: yes
  
  vars:
    test_results: []
    
  tasks:
    - name: 初始化测试结果
      set_fact:
        test_results: []
        
    - name: 测试Nmap
      block:
        - name: 检查Nmap是否安装
          command: "which nmap"
          register: nmap_check
          ignore_errors: yes
          changed_when: false
          
        - name: 测试Nmap基本功能
          command: "nmap -sn 127.0.0.1"
          register: nmap_test
          when: nmap_check.rc == 0
          ignore_errors: yes
          changed_when: false
          
        - name: 记录Nmap测试结果
          set_fact:
            test_results: "{{ test_results + ['Nmap: ' + ('✓ 正常工作' if nmap_check.rc == 0 and nmap_test.rc == 0 else '✗ 未安装或异常')] }}"
          
    - name: 测试Metasploit Framework
      block:
        - name: 检查Metasploit是否安装
          command: "which msfconsole"
          register: msf_check
          ignore_errors: yes
          changed_when: false
          
        - name: 测试Metasploit版本
          command: "msfconsole --version"
          register: msf_version
          when: msf_check.rc == 0
          ignore_errors: yes
          changed_when: false
          
        - name: 记录Metasploit测试结果
          set_fact:
            test_results: "{{ test_results + ['Metasploit: ' + ('✓ 正常工作' if msf_check.rc == 0 else '✗ 未安装')] }}"
          
    - name: 测试Wireshark
      block:
        - name: 检查Wireshark是否安装
          command: "which wireshark"
          register: wireshark_check
          ignore_errors: yes
          changed_when: false
          
        - name: 检查tshark是否可用
          command: "which tshark"
          register: tshark_check
          when: wireshark_check.rc == 0
          ignore_errors: yes
          changed_when: false
          
        - name: 记录Wireshark测试结果
          set_fact:
            test_results: "{{ test_results + ['Wireshark: ' + ('✓ 正常工作' if wireshark_check.rc == 0 else '✗ 未安装')] }}"
          
    - name: 测试John the Ripper
      block:
        - name: 检查John是否安装
          command: "which john"
          register: john_check
          ignore_errors: yes
          changed_when: false
          
        - name: 测试John版本
          command: "john --version"
          register: john_version
          when: john_check.rc == 0
          ignore_errors: yes
          changed_when: false
          
        - name: 记录John测试结果
          set_fact:
            test_results: "{{ test_results + ['John the Ripper: ' + ('✓ 正常工作' if john_check.rc == 0 else '✗ 未安装')] }}"
          
    - name: 测试Hydra
      block:
        - name: 检查Hydra是否安装
          command: "which hydra"
          register: hydra_check
          ignore_errors: yes
          changed_when: false
          
        - name: 测试Hydra帮助
          command: "hydra -h"
          register: hydra_help
          when: hydra_check.rc == 0
          ignore_errors: yes
          changed_when: false
          
        - name: 记录Hydra测试结果
          set_fact:
            test_results: "{{ test_results + ['Hydra: ' + ('✓ 正常工作' if hydra_check.rc == 0 else '✗ 未安装')] }}"
          
    - name: 测试SQLMap
      block:
        - name: 检查SQLMap是否安装
          command: "which sqlmap"
          register: sqlmap_check
          ignore_errors: yes
          changed_when: false
          
        - name: 测试SQLMap版本
          command: "sqlmap --version"
          register: sqlmap_version
          when: sqlmap_check.rc == 0
          ignore_errors: yes
          changed_when: false
          
        - name: 记录SQLMap测试结果
          set_fact:
            test_results: "{{ test_results + ['SQLMap: ' + ('✓ 正常工作' if sqlmap_check.rc == 0 else '✗ 未安装')] }}"
          
    - name: 测试Aircrack-ng
      block:
        - name: 检查Aircrack-ng是否安装
          command: "which airmon-ng"
          register: aircrack_check
          ignore_errors: yes
          changed_when: false
          
        - name: 测试Aircrack-ng版本
          command: "airmon-ng --version"
          register: aircrack_version
          when: aircrack_check.rc == 0
          ignore_errors: yes
          changed_when: false
          
        - name: 记录Aircrack-ng测试结果
          set_fact:
            test_results: "{{ test_results + ['Aircrack-ng: ' + ('✓ 正常工作' if aircrack_check.rc == 0 else '✗ 未安装')] }}"
          
    - name: 测试Burp Suite
      block:
        - name: 检查Burp Suite是否安装
          command: "which burpsuite"
          register: burp_check
          ignore_errors: yes
          changed_when: false
          
        - name: 记录Burp Suite测试结果
          set_fact:
            test_results: "{{ test_results + ['Burp Suite: ' + ('✓ 正常工作' if burp_check.rc == 0 else '✗ 未安装')] }}"
          
    - name: 测试Netcat
      block:
        - name: 检查Netcat是否安装
          command: "which nc"
          register: nc_check
          ignore_errors: yes
          changed_when: false
          
        - name: 测试Netcat基本功能
          command: "nc -z 127.0.0.1 22"
          register: nc_test
          when: nc_check.rc == 0
          ignore_errors: yes
          changed_when: false
          
        - name: 记录Netcat测试结果
          set_fact:
            test_results: "{{ test_results + ['Netcat: ' + ('✓ 正常工作' if nc_check.rc == 0 else '✗ 未安装')] }}"
          
    - name: 测试Curl
      block:
        - name: 检查Curl是否安装
          command: "which curl"
          register: curl_check
          ignore_errors: yes
          changed_when: false
          
        - name: 测试Curl基本功能
          command: "curl -s -o /dev/null -w '%{http_code}' https://httpbin.org/get"
          register: curl_test
          when: curl_check.rc == 0
          ignore_errors: yes
          changed_when: false
          
        - name: 记录Curl测试结果
          set_fact:
            test_results: "{{ test_results + ['Curl: ' + ('✓ 正常工作' if curl_check.rc == 0 else '✗ 未安装')] }}"
          
    - name: 显示所有测试结果
      debug:
        msg: |
          === Kali Linux 安全工具测试结果 ===
          {% for result in test_results %}
          {{ result }}
          {% endfor %}
          
    - name: 生成测试报告
      copy:
        content: |
          # Kali Linux 安全工具测试报告
          
          **测试时间**: {{ ansible_date_time.iso8601 }}
          **测试主机**: {{ ansible_hostname }}
          
          ## 测试结果
          
          {% for result in test_results %}
          {{ result }}
          {% endfor %}
          
          ## 建议
          
          {% for result in test_results %}
          {% if '✗' in result %}
          - {{ result.split(':')[0] }} 未安装，可以使用以下命令安装: `sudo apt install {{ result.split(':')[0].lower().replace(' ', '-') }}`
          {% endif %}
          {% endfor %}
          
        dest: "/tmp/kali_security_tools_report.md"