Files
infra-automation/roles/system_info/tasks/gather_memory.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

110 lines
4.3 KiB
YAML

---
# Memory information gathering tasks
- name: Gather memory information from Ansible facts
set_fact:
system_info_memory_total_mb: "{{ ansible_memtotal_mb }}"
system_info_memory_free_mb: "{{ ansible_memfree_mb }}"
system_info_swap_total_mb: "{{ ansible_swaptotal_mb }}"
system_info_swap_free_mb: "{{ ansible_swapfree_mb }}"
tags: [gather, memory]
- name: Gather detailed memory information
shell: free -h
register: system_info_free_raw
changed_when: false
tags: [gather, memory]
- name: Gather memory information from /proc/meminfo
shell: cat /proc/meminfo | grep -iE "MemTotal|MemFree|MemAvailable|Buffers|Cached|SwapTotal|SwapFree|SwapCached"
register: system_info_meminfo_raw
changed_when: false
tags: [gather, memory]
- name: Parse detailed memory values
set_fact:
system_info_mem_available_kb: "{{ system_info_meminfo_raw.stdout | regex_search('MemAvailable:\\s+(\\d+)', '\\1') | default(['0'], true) | first }}"
system_info_mem_buffers_kb: "{{ system_info_meminfo_raw.stdout | regex_search('Buffers:\\s+(\\d+)', '\\1') | default(['0'], true) | first }}"
system_info_mem_cached_kb: "{{ system_info_meminfo_raw.stdout | regex_search('Cached:\\s+(\\d+)', '\\1') | default(['0'], true) | first }}"
tags: [gather, memory]
- name: Gather physical memory hardware information
shell: dmidecode -t memory | grep -iE "Size|Type|Speed|Manufacturer|Serial Number|Locator" || echo "DMI memory info not available"
register: system_info_dmi_memory_raw
changed_when: false
become: true
failed_when: false
tags: [gather, memory]
- name: Count physical memory modules
shell: dmidecode -t memory | grep -c "^[[:space:]]*Size.*MB" || echo "0"
register: system_info_memory_modules_count_raw
changed_when: false
become: true
failed_when: false
tags: [gather, memory]
- name: Gather swap devices information
shell: swapon --show --noheadings || echo "No swap configured"
register: system_info_swap_devices_raw
changed_when: false
tags: [gather, memory]
- name: Calculate memory usage percentage
set_fact:
system_info_memory_used_mb: "{{ ansible_memtotal_mb - ansible_memfree_mb }}"
system_info_memory_usage_percent: "{{ ((ansible_memtotal_mb - ansible_memfree_mb) / ansible_memtotal_mb * 100) | round(2) }}"
tags: [gather, memory]
- name: Calculate swap usage percentage
set_fact:
system_info_swap_used_mb: "{{ ansible_swaptotal_mb - ansible_swapfree_mb }}"
system_info_swap_usage_percent: "{{ ((ansible_swaptotal_mb - ansible_swapfree_mb) / ansible_swaptotal_mb * 100) | round(2) if ansible_swaptotal_mb > 0 else 0 }}"
tags: [gather, memory]
- name: Check for memory pressure
shell: |
if [ -f /proc/pressure/memory ]; then
cat /proc/pressure/memory
else
echo "Memory pressure statistics not available"
fi
register: system_info_memory_pressure_raw
changed_when: false
failed_when: false
tags: [gather, memory]
- name: Gather huge pages information
shell: |
grep -E "HugePages_|Hugepagesize" /proc/meminfo || echo "Huge pages not configured"
register: system_info_hugepages_raw
changed_when: false
tags: [gather, memory]
- name: Aggregate memory information
set_fact:
system_info_memory:
total_mb: "{{ system_info_memory_total_mb }}"
free_mb: "{{ system_info_memory_free_mb }}"
used_mb: "{{ system_info_memory_used_mb }}"
available_kb: "{{ system_info_mem_available_kb }}"
buffers_kb: "{{ system_info_mem_buffers_kb }}"
cached_kb: "{{ system_info_mem_cached_kb }}"
usage_percent: "{{ system_info_memory_usage_percent }}"
physical_modules: "{{ system_info_memory_modules_count_raw.stdout | default('0') }}"
hardware_details: "{{ system_info_dmi_memory_raw.stdout_lines | default([]) }}"
pressure: "{{ system_info_memory_pressure_raw.stdout_lines | default([]) }}"
hugepages: "{{ system_info_hugepages_raw.stdout_lines | default([]) }}"
free_output: "{{ system_info_free_raw.stdout_lines }}"
tags: [gather, memory]
- name: Aggregate swap information
set_fact:
system_info_swap:
total_mb: "{{ system_info_swap_total_mb }}"
free_mb: "{{ system_info_swap_free_mb }}"
used_mb: "{{ system_info_swap_used_mb }}"
usage_percent: "{{ system_info_swap_usage_percent }}"
devices: "{{ system_info_swap_devices_raw.stdout_lines | default([]) }}"
tags: [gather, memory]