Files
infra-automation/roles/system_info/tasks/detect_hypervisor.yml
ansible 70b57d223f Add system_info role for comprehensive infrastructure inventory
New role for gathering detailed system information including CPU, GPU,
RAM, disk, network, and hypervisor details with JSON export capabilities.

Role capabilities:
- Comprehensive hardware detection (CPU, GPU, RAM, disk, network)
- Hypervisor detection (KVM, Proxmox, LXD, Docker, Podman, VMware, Hyper-V)
- System information gathering (OS, kernel, uptime, security modules)
- Health checks and validation tasks
- JSON export with timestamped backups
- Human-readable summary generation
- Support for multiple Linux distributions

Features:
- Modular task organization by information type
- Feature toggles for selective gathering
- CLAUDE.md compliant validation tasks including:
  * Disk usage monitoring (>80% warnings)
  * Memory usage statistics
  * Top CPU and memory processes
  * System uptime tracking
  * Logged users reporting
- OS-specific variable handling
- DMI/SMBIOS hardware information
- SMART disk health status
- Network interface statistics

File structure:
roles/system_info/
├── README.md              # Comprehensive documentation
├── defaults/main.yml      # Configurable defaults
├── vars/main.yml          # Role variables
├── meta/main.yml          # Galaxy metadata
├── tasks/
│   ├── main.yml          # Main task coordinator
│   ├── install.yml       # Package installation
│   ├── gather_system.yml # OS and system info
│   ├── gather_cpu.yml    # CPU details
│   ├── gather_gpu.yml    # GPU detection
│   ├── gather_memory.yml # RAM information
│   ├── gather_disk.yml   # Disk and LVM info
│   ├── gather_network.yml # Network configuration
│   ├── detect_hypervisor.yml # Virtualization detection
│   ├── export_stats.yml  # JSON export
│   └── validate.yml      # Health checks (CLAUDE.md compliant)
├── templates/
│   └── summary.txt.j2    # Human-readable summary
├── handlers/
│   └── main.yml          # Service handlers
└── tests/
    └── test.yml          # Basic test playbook

Use cases:
- Infrastructure inventory for CMDB integration
- Capacity planning and resource optimization
- Hardware audit and compliance reporting
- Hypervisor and VM tracking
- System health monitoring
- Documentation generation

Output:
- JSON: ./stats/machines/<fqdn>/system_info.json
- Backup: ./stats/machines/<fqdn>/system_info_<timestamp>.json
- Summary: ./stats/machines/<fqdn>/summary.txt

Requirements:
- Ansible >= 2.9
- Root/sudo access for hardware information
- Packages: lshw, dmidecode, pciutils, usbutils, smartmontools, ethtool

Compliance:
- CLAUDE.md health check requirements implemented
- CIS Benchmark support for system auditing
- NIST compliance documentation support
- Security-first design with minimal system impact

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-11 01:36:01 +01:00

285 lines
11 KiB
YAML

---
# Hypervisor detection tasks
- name: Check if running in a virtual environment
set_fact:
system_info_virtualization_type: "{{ ansible_virtualization_type | default('physical') }}"
system_info_virtualization_role: "{{ ansible_virtualization_role | default('NA') }}"
tags: [gather, hypervisor]
- name: Detect virtualization using systemd-detect-virt
shell: systemd-detect-virt
register: system_info_detect_virt_raw
changed_when: false
failed_when: false
tags: [gather, hypervisor]
- name: Set systemd virtualization detection
set_fact:
system_info_systemd_virt: "{{ system_info_detect_virt_raw.stdout | default('none') }}"
tags: [gather, hypervisor]
- name: Check for KVM/QEMU hypervisor capability
shell: |
if command -v virsh &> /dev/null; then
echo "virsh: available"
virsh version 2>/dev/null || echo "virsh not accessible"
else
echo "virsh: not installed"
fi
register: system_info_virsh_check_raw
changed_when: false
failed_when: false
tags: [gather, hypervisor, kvm]
- name: Check libvirt service status
shell: systemctl is-active libvirtd 2>/dev/null || echo "not running"
register: system_info_libvirtd_status_raw
changed_when: false
failed_when: false
tags: [gather, hypervisor, libvirt]
- name: Gather libvirt details (if available)
block:
- name: Check libvirt version
shell: virsh version
register: system_info_libvirt_version_raw
changed_when: false
become: true
- name: List libvirt networks
shell: virsh net-list --all
register: system_info_libvirt_networks_raw
changed_when: false
become: true
- name: List libvirt storage pools
shell: virsh pool-list --all
register: system_info_libvirt_pools_raw
changed_when: false
become: true
- name: Count running VMs
shell: virsh list --state-running | grep -c running || echo "0"
register: system_info_libvirt_running_vms_raw
changed_when: false
become: true
- name: Count total VMs
shell: virsh list --all | tail -n +3 | grep -v "^$" | wc -l
register: system_info_libvirt_total_vms_raw
changed_when: false
become: true
when: "'available' in system_info_virsh_check_raw.stdout"
failed_when: false
tags: [gather, hypervisor, libvirt]
- name: Check for Proxmox VE
shell: |
if command -v pveversion &> /dev/null; then
pveversion
else
echo "Proxmox VE not installed"
fi
register: system_info_proxmox_check_raw
changed_when: false
failed_when: false
tags: [gather, hypervisor, proxmox]
- name: Gather Proxmox details (if available)
block:
- name: Get Proxmox cluster status
shell: pvecm status 2>/dev/null || echo "Not in a cluster"
register: system_info_proxmox_cluster_raw
changed_when: false
- name: List Proxmox VMs
shell: qm list 2>/dev/null || echo "No VMs or qm not available"
register: system_info_proxmox_vms_raw
changed_when: false
- name: List Proxmox containers
shell: pct list 2>/dev/null || echo "No containers or pct not available"
register: system_info_proxmox_containers_raw
changed_when: false
- name: Get Proxmox storage status
shell: pvesm status 2>/dev/null || echo "Storage information not available"
register: system_info_proxmox_storage_raw
changed_when: false
when: "'pveversion' in system_info_proxmox_check_raw.stdout"
failed_when: false
tags: [gather, hypervisor, proxmox]
- name: Check for LXD/LXC
shell: |
if command -v lxc &> /dev/null; then
lxc version
else
echo "LXD not installed"
fi
register: system_info_lxd_check_raw
changed_when: false
failed_when: false
tags: [gather, hypervisor, lxd]
- name: Gather LXD details (if available)
block:
- name: List LXD containers
shell: lxc list --format json
register: system_info_lxd_containers_raw
changed_when: false
- name: Get LXD storage pools
shell: lxc storage list --format json
register: system_info_lxd_storage_raw
changed_when: false
- name: Get LXD networks
shell: lxc network list --format json
register: system_info_lxd_networks_raw
changed_when: false
- name: Check LXD cluster status
shell: lxc cluster list --format json 2>/dev/null || echo "[]"
register: system_info_lxd_cluster_raw
changed_when: false
when: "'Client version' in system_info_lxd_check_raw.stdout or 'Server version' in system_info_lxd_check_raw.stdout"
failed_when: false
tags: [gather, hypervisor, lxd]
- name: Check for Docker
shell: |
if command -v docker &> /dev/null; then
docker version --format '{{.Server.Version}}' 2>/dev/null || echo "Docker installed but not running"
else
echo "Docker not installed"
fi
register: system_info_docker_check_raw
changed_when: false
failed_when: false
tags: [gather, hypervisor, docker]
- name: Gather Docker details (if available)
block:
- name: Count running containers
shell: docker ps -q | wc -l
register: system_info_docker_running_raw
changed_when: false
- name: Count total containers
shell: docker ps -aq | wc -l
register: system_info_docker_total_raw
changed_when: false
- name: List Docker images
shell: docker images --format "{{.Repository}}:{{.Tag}}" | wc -l
register: system_info_docker_images_raw
changed_when: false
when:
- "'not installed' not in system_info_docker_check_raw.stdout"
- "'not running' not in system_info_docker_check_raw.stdout"
failed_when: false
tags: [gather, hypervisor, docker]
- name: Check for Podman
shell: |
if command -v podman &> /dev/null; then
podman version --format '{{.Version}}'
else
echo "Podman not installed"
fi
register: system_info_podman_check_raw
changed_when: false
failed_when: false
tags: [gather, hypervisor, podman]
- name: Gather VMware ESXi/vSphere information
shell: |
if [ -f /etc/vmware-release ]; then
cat /etc/vmware-release
else
echo "Not VMware ESXi"
fi
register: system_info_vmware_check_raw
changed_when: false
failed_when: false
tags: [gather, hypervisor, vmware]
- name: Check for Hyper-V Linux Integration Services
shell: |
if lsmod | grep -q hv_vmbus; then
echo "Hyper-V detected"
lsmod | grep ^hv_
else
echo "Not Hyper-V"
fi
register: system_info_hyperv_check_raw
changed_when: false
failed_when: false
tags: [gather, hypervisor, hyperv]
- name: Determine if system is a hypervisor
set_fact:
system_info_is_hypervisor: >-
{{
('available' in system_info_virsh_check_raw.stdout) or
('pveversion' in system_info_proxmox_check_raw.stdout) or
('Client version' in system_info_lxd_check_raw.stdout) or
('Server version' in system_info_lxd_check_raw.stdout) or
('VMware ESXi' in system_info_vmware_check_raw.stdout) or
(system_info_docker_check_raw.stdout | regex_search('\\d+\\.\\d+'))
}}
tags: [gather, hypervisor]
- name: Aggregate hypervisor information
set_fact:
system_info_hypervisor:
is_virtual: "{{ system_info_virtualization_role == 'guest' }}"
is_hypervisor: "{{ system_info_is_hypervisor }}"
virtualization_type: "{{ system_info_virtualization_type }}"
virtualization_role: "{{ system_info_virtualization_role }}"
systemd_detection: "{{ system_info_systemd_virt }}"
kvm_libvirt:
installed: "{{ 'available' in system_info_virsh_check_raw.stdout }}"
service_status: "{{ system_info_libvirtd_status_raw.stdout | default('N/A') }}"
version: "{{ system_info_libvirt_version_raw.stdout_lines | default([]) if 'available' in system_info_virsh_check_raw.stdout else [] }}"
running_vms: "{{ system_info_libvirt_running_vms_raw.stdout | default('0') if 'available' in system_info_virsh_check_raw.stdout else '0' }}"
total_vms: "{{ system_info_libvirt_total_vms_raw.stdout | default('0') if 'available' in system_info_virsh_check_raw.stdout else '0' }}"
networks: "{{ system_info_libvirt_networks_raw.stdout_lines | default([]) if 'available' in system_info_virsh_check_raw.stdout else [] }}"
storage_pools: "{{ system_info_libvirt_pools_raw.stdout_lines | default([]) if 'available' in system_info_virsh_check_raw.stdout else [] }}"
proxmox:
installed: "{{ 'pveversion' in system_info_proxmox_check_raw.stdout }}"
version: "{{ system_info_proxmox_check_raw.stdout | default('N/A') }}"
cluster_status: "{{ system_info_proxmox_cluster_raw.stdout_lines | default([]) if 'pveversion' in system_info_proxmox_check_raw.stdout else [] }}"
vms: "{{ system_info_proxmox_vms_raw.stdout_lines | default([]) if 'pveversion' in system_info_proxmox_check_raw.stdout else [] }}"
containers: "{{ system_info_proxmox_containers_raw.stdout_lines | default([]) if 'pveversion' in system_info_proxmox_check_raw.stdout else [] }}"
storage: "{{ system_info_proxmox_storage_raw.stdout_lines | default([]) if 'pveversion' in system_info_proxmox_check_raw.stdout else [] }}"
lxd:
installed: "{{ 'version' in system_info_lxd_check_raw.stdout }}"
version: "{{ system_info_lxd_check_raw.stdout | default('N/A') }}"
containers: "{{ system_info_lxd_containers_raw.stdout | default('[]') if 'version' in system_info_lxd_check_raw.stdout else '[]' }}"
storage: "{{ system_info_lxd_storage_raw.stdout | default('[]') if 'version' in system_info_lxd_check_raw.stdout else '[]' }}"
networks: "{{ system_info_lxd_networks_raw.stdout | default('[]') if 'version' in system_info_lxd_check_raw.stdout else '[]' }}"
cluster: "{{ system_info_lxd_cluster_raw.stdout | default('[]') if 'version' in system_info_lxd_check_raw.stdout else '[]' }}"
docker:
installed: "{{ 'not installed' not in system_info_docker_check_raw.stdout }}"
version: "{{ system_info_docker_check_raw.stdout | default('N/A') }}"
running_containers: "{{ system_info_docker_running_raw.stdout | default('0') if 'not installed' not in system_info_docker_check_raw.stdout and 'not running' not in system_info_docker_check_raw.stdout else '0' }}"
total_containers: "{{ system_info_docker_total_raw.stdout | default('0') if 'not installed' not in system_info_docker_check_raw.stdout and 'not running' not in system_info_docker_check_raw.stdout else '0' }}"
images_count: "{{ system_info_docker_images_raw.stdout | default('0') if 'not installed' not in system_info_docker_check_raw.stdout and 'not running' not in system_info_docker_check_raw.stdout else '0' }}"
podman:
installed: "{{ 'not installed' not in system_info_podman_check_raw.stdout }}"
version: "{{ system_info_podman_check_raw.stdout | default('N/A') }}"
vmware:
is_esxi: "{{ 'VMware ESXi' in system_info_vmware_check_raw.stdout }}"
version: "{{ system_info_vmware_check_raw.stdout | default('N/A') }}"
hyperv:
detected: "{{ 'Hyper-V detected' in system_info_hyperv_check_raw.stdout }}"
modules: "{{ system_info_hyperv_check_raw.stdout_lines | default([]) }}"
tags: [gather, hypervisor]