鲲鹏社区首页
中文
注册
开发者
我要评分
获取效率
正确性
完整性
易理解
在线提单
论坛求助

使用Ansible远端采集集群信息

使用Ansible自动部署并运行鲲鹏健康检测工具(Kspect),完成服务器硬件数据采集并下载报告;若有服务器运维经验,可尝试使用此示例的使用方式。

前提条件

  • 服务器已安装Ansible 2.9或以上版本。
  • 配置SSH密钥认证到目标服务器。
  • 已准备鲲鹏健康检测工具压缩包(devkit-kspect-x.x.x-Linux-aarch64.tar.gz)。
  • 目标服务器为鲲鹏服务器且支持SSH访问。

采集集群硬件信息

  1. 创建目录。
    1
    mkdir -p /home/kspect-ansible/{group_vars,files,templates}
    
  2. 将鲲鹏健康诊断工具压缩包放入“/home/kspect-ansible/files/”目录 。
    1
    cp /path/to/devkit-kspect-x.x.x-Linux-aarch64.tar.gz /home/kspect-ansible/files/
    
  3. 切换至“/home/kspect-ansible”目录。
    1
    cd /home/kspect-ansible
    
  4. 创建并完善主机清单文件(inventory.ini)。
    1
    vi inventory.ini 
    
    • 若不采集BMC信息,可不填写BMC账户和密码。
    • 服务器密码和BMC密码以明文形式存储在inventory文件中,建议使用Ansible Vault加密。
    • 建议定期更新BMC账户密码。

    先按“i”进入编辑模式,文件内容如下:

    [servers]
    server1 ansible_host=xxx.xxx.xxx.xxx ansible_user=root
    server2 ansible_host=xxx.xxx.xxx.xxx ansible_user=root bmc_ip=xxx.xxx.xxx.xxx bmc_user=Administrator bmc_password=xxxx
    [servers:vars]
    kspect_package=devkit-kspect-x.x.x-Linux-aarch64.tar.gz
    kspect_dir=devkit-kspect-x.x.x-Linux-aarch64
    install_dir=/tmp/kspect_collect

    完成文件后按“Esc”键,输入:wq!,按“Enter”保存并退出编辑。

  5. 创建并完善采集脚本文件(kspect_no_bmc.exp和kspect_with_bmc.exp)。
    • 不采集BMC信息。
      1
      vi templates/kspect_no_bmc.exp
      

      先按“i”进入编辑模式,文件内容如下:

      #!/usr/bin/expect -f
      set timeout 1800  ;
      spawn ./kspect -l 0 all
      expect {
          "Are you sure you want to enter (y/N)" {
              send "N\r"
              exp_continue
          }
          eof {
              exit 0
          }
          timeout {
              send_user "timeout"
              exit 1
          }
      }

      完成文件后按“Esc”键,输入:wq!,按“Enter”保存并退出编辑。

    • 采集BMC信息。
      1
      vi templates/kspect_with_bmc.exp
      

      先按“i”进入编辑模式,文件内容如下:

      #!/usr/bin/expect -f
      set timeout 1800
      set bmc_ip [lindex $argv 0]
      set bmc_user [lindex $argv 1]
      set bmc_password [lindex $argv 2]
      set timeout 1800  ;
      spawn ./kspect -l 0 all
      expect {
          "Are you sure you want to enter (y/N)" {
              send "y\r"
              exp_continue
          }
          "BMC IP address:" {
              send "$bmc_ip\r"
              exp_continue
          }
          "Username:" {
              send "$bmc_user\r"
              exp_continue
          }
          "Password:" {
              send "$bmc_password\r"
              exp_continue
          }
          "BMC manager dump? (y/N)" {
              send "N\r"
              exp_continue
          }
          eof {
              exit 0
          }
          timeout {
              send_user "timeout"
              exit 1
          }
      }

      完成文件后按“Esc”键,输入:wq!,按“Enter”保存并退出编辑。

  6. 创建并完善主playbook配置文件(kspect_collect.yml)。
    1
    vi kspect_collect.yml
    

    先按“i”进入编辑模式,文件内容如下:

    - name: Kspect远端数据采集
      hosts: servers
      gather_facts: yes
      become: yes
      tasks:
        - name: 检查目标服务器架构
          fail:
            msg: "此工具仅支持鲲鹏架构服务器"
          when: ansible_architecture != "aarch64"
        - name: 创建安装目录
          file:
            path: "{{ install_dir }}"
            state: directory
            mode: '0700'
        - name: 上传Kspect压缩包
          copy:
            src: "files/{{ kspect_package }}"
            dest: "{{ install_dir }}/{{ kspect_package }}"
            mode: '0600'
        - name: 解压Kspect工具包
          unarchive:
            src: "{{ install_dir }}/{{ kspect_package }}"
            dest: "{{ install_dir }}"
            copy: no
            mode: '0700'
        - name: 查找Kspect解压后的目录
          find:
            paths: "{{ install_dir }}"
            patterns: "kspect*,devkit*"
            file_type: directory
          register: kspect_dirs
        - name: 设置Kspect目录变量
          set_fact:
            kspect_dir: "{{ kspect_dirs.files[0].path | basename }}"
          when: kspect_dirs.files | length > 0
        - name: 显示Kspect目录
          debug:
            var: kspect_dir
        - name: 检查是否配置了BMC信息
          set_fact:
            has_bmc_info: "{{ bmc_ip is defined and bmc_ip | length > 0 }}"
        - name: 显示BMC配置状态
          debug:
            msg: "{{ '配置了BMC信息,将采集BMC数据' if has_bmc_info else '未配置BMC信息,将跳过BMC采集' }}"
        - name: 创建Expect交互脚本
          template:
            src: "{{ 'kspect_with_bmc.exp' if has_bmc_info else 'kspect_no_bmc.exp' }}"
            dest: "{{ install_dir }}/{{ kspect_dir }}/run_kspect.exp"
            mode: '0700'
        - name: 检查output目录是否存在
          stat:
            path: "{{ install_dir }}/{{ kspect_dir }}/output"
          register: output_dir
        - name: 创建output目录(如果不存在)
          file:
            path: "{{ install_dir }}/{{ kspect_dir }}/output"
            state: directory
            mode: '0700'
          when: not output_dir.stat.exists
        - name: 运行Kspect采集
          shell: |
            cd "{{ install_dir }}/{{ kspect_dir }}"
            {{ 'expect run_kspect.exp "' ~ bmc_ip ~ '" "' ~ bmc_user ~ '" "' ~ bmc_password ~ '"' if has_bmc_info else './kspect -s all' }}
          args:
            executable: /bin/bash
          environment:
            TERM: xterm
          async: 1200
          poll: 30
          register: kspect_job
        - name: 等待Kspect采集完成
          async_status:
            jid: "{{ kspect_job.ansible_job_id }}"
          register: kspect_result
          until: kspect_result.finished
          retries: 30
          delay: 10
        - name: 检查Kspect执行状态
          debug:
            var: kspect_result
        - name: 检查报告文件是否存在
          shell: |
            ls -la "{{ install_dir }}/{{ kspect_dir }}/output/" || echo "目录不存在"
            find "{{ install_dir }}/{{ kspect_dir }}/output" -name "*.tar.gz" -type f 2>/dev/null || echo "未找到报告文件"
          register: direct_file_check
        - name: 显示文件检查结果
          debug:
            var: direct_file_check.stdout
        - name: 查找报告文件
          shell: |
            find "{{ install_dir }}/{{ kspect_dir }}/output" -name "kspect-report-*.tar.gz" -type f
          register: shell_find_files
        - name: 显示查找结果
          debug:
            var: shell_find_files.stdout_lines
        - name: 设置报告文件路径
          set_fact:
            report_file_path: "{{ shell_find_files.stdout_lines[0] }}"
          when: shell_find_files.stdout_lines | length > 0
        - name: 验证报告文件
          stat:
            path: "{{ report_file_path }}"
          register: report_stat
          when: report_file_path is defined
        - name: 显示报告文件状态
          debug:
            var: report_stat
          when: report_file_path is defined
        - name: 创建本地报告目录
          delegate_to: localhost
          run_once: yes
          file:
            path: "reports/{{ inventory_hostname }}"
            state: directory
        - name: 下载采集报告
          fetch:
            src: "{{ report_file_path }}"
            dest: "reports/{{ inventory_hostname }}/"
            flat: yes
          when: report_file_path is defined and report_stat.stat.exists
        - name: 验证报告是否下载
          delegate_to: localhost
          stat:
            path: "reports/{{ inventory_hostname }}/{{ (report_file_path | basename) }}"
          register: downloaded_report
          ignore_errors: yes
          when: report_file_path is defined
        - name: 显示下载结果
          debug:
            msg: "报告下载状态: {{ downloaded_report.stat.exists if downloaded_report is defined else '未知' }}"
          when: report_file_path is defined
        - name: 清理远程文件
          file:
            path: "{{ install_dir }}"
            state: absent
        - name: 验证清理结果
          shell: |
            if [ -d "{{ install_dir }}" ]; then
              echo "目录仍存在"
              exit 1
            else
              echo "目录已删除"
              exit 0
            fi
          register: cleanup_check
          failed_when: cleanup_check.stdout == "目录仍存在"
          ignore_errors: yes
    
        - name: 显示各服务器采集结果
          debug:
            msg: "{{ s }}"
          vars:
            s: >-
              {% if (downloaded_report.stat.exists | default(false)) %}
              服务器{{ inventory_hostname }}采集成功,报告位置为reports/{{ inventory_hostname }}/{{ (report_file_path | basename) }}
              {% else %}
              服务器{{ inventory_hostname }}采集失败
              {% endif %}

    完成文件后按“Esc”键,输入:wq!,按“Enter”保存并退出编辑。

  7. 执行采集任务。
    1
    ansible-playbook -i inventory.ini kspect_collect.yml
    

    回显中将提示采集的报告压缩包保存目录。

  8. 查看报告文件的保存目录。
    通过查看文件目录结构,查看采集的压缩包存放路径。
    1
    tree /home/kspect-ansible
    

    在reports目录下保存有采集的报告,返回信息如下:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    kspect-ansible/
    ├── inventory.ini
    ├── kspect_collect.yml
    ├── templates/
       ├── kspect_with_bmc.exp
       └── kspect_no_bmc.exp
    ├── reports
       └── server1
            └── kspect-report-2025xxxx-xxxxx1.tar.gz
       └── server2
            └── kspect-report-2025xxxx-xxxxx2.tar.gz
    └── files/
        └── devkit-kspect-x.x.x-Linux-aarch64.tar.gz
    
  9. 解压报告压缩包。
    1
    2
    3
    cd /home/kspect-ansible/reports/server1
    tar -zxvf kspect-report-xxxxxxxx-xxxxxx.tar.gz
    cd  kspect-report-xxxxxxxx-xxxxxx
    

    采集到的报告压缩包中包含全量信息文件(HTML/JSON/CSV)和dmesg日志文件,其中HTML文件可使用浏览器查看,JSON文件可使用kspect工具查看。

  10. 将结果文件复制到kspect的报告目录。
    1
    cp -r kspect-report-xxxxxxxx-xxxxxx /home/devkit-kspect-x.x.x-Linux-aarch64/output
    
  11. 使用kspect查看报告。

    可先使用-h参数查看报告序号。

    1
    2
    cd /home/devkit-kspect-x.x.x-Linux-aarch64
    ./kspect report -h
    

    返回信息如下:

    1
    2
    3
    4
    5
    6
    7
    8
    ...
    ...
        -r REPORT, --report REPORT
        选择展示哪一份报告(或自行输入报告路径,支持工具采集报告和使用diff/diff_x86后生成的对比报告):
        1: /home/devkit-kspect-x.x.x-Linux-aarch64/output/kspect-report-20251119-105559/kspect-json-20251119-105559.json <2025-11-19 10:55:59>
        2: /home/devkit-kspect-x.x.x-Linux-aarch64/output/kspect-report-20251119-105135/kspect-json-20251119-105135.json <2025-11-19 10:54:01>
    ...
    ...
    

    再使用-r参数查看指定报告。

    1
    ./kspect report -r 1
    
    图1 报告信息
  12. 对比不同服务器报告。
    1
    ./kspect report -d 1,2
    
    图2 存储部分

    对比报告中将标红差值超过阈值(默认20%)的数据项,同样软硬件的不同配置也将标红,便于后续统一配置等操作。