-----
- name: Hardware Specification Collection and Analysis
hosts: all
gather_facts: yes
vars:
hw_specs: []
tasks:
- name: Get CPU information
shell: nproc
register: cpu_count
- name: Get memory information (GB)
shell: free -g | grep ‘^Mem:’ | awk ‘{print $2}’
register: memory_gb
- name: Get disk information
shell: |
lsblk -b -o NAME,SIZE,TYPE,MOUNTPOINT –json | jq -r ’
.blockdevices[] |
select(.type==“disk”) |
.name + “:” + (.size|tonumber/1024/1024/1024|floor|tostring) + “GB”
’
register: disk_info
- name: Get unmounted partitions
shell: |
lsblk -b -o NAME,SIZE,TYPE,MOUNTPOINT –json | jq -r ’
.blockdevices[] |
.. |
select(.type?==“part” and .mountpoint==null) |
.name + “:” + (.size|tonumber/1024/1024/1024|floor|tostring) + “GB”
’
register: unmounted_partitions
failed_when: false
- name: Collect hardware specs
set_fact:
current_hw_spec:
hostname: “{{ inventory_hostname }}”
cpu_count: “{{ cpu_count.stdout | int }}”
memory_gb: “{{ memory_gb.stdout | int }}”
disks: “{{ disk_info.stdout_lines }}”
unmounted_partitions: “{{ unmounted_partitions.stdout_lines | default([]) }}”
- name: Add to hw_specs list
set_fact:
hw_specs: “{{ hw_specs + [current_hw_spec] }}”
delegate_to: localhost
delegate_facts: true
run_once: false
- name: Analyze and Display Results
hosts: localhost
gather_facts: no
vars:
cpu_groups: {}
memory_groups: {}
tasks:
- name: Group by CPU count
set_fact:
cpu_groups: “{{ cpu_groups | combine({item.cpu_count | string: (cpu_groups[item.cpu_count | string] | default([])) + [item.hostname]}) }}”
loop: “{{ hw_specs }}”
- name: Group by Memory size
set_fact:
memory_groups: “{{ memory_groups | combine({item.memory_gb | string: (memory_groups[item.memory_gb | string] | default([])) + [item.hostname]}) }}”
loop: “{{ hw_specs }}”
- name: Display Hardware Specification Summary
debug:
msg: |
```
================================================
HARDWARE SPECIFICATION COLLECTION SUMMARY
================================================
Total Nodes Checked: {{ hw_specs | length }}
CPU COUNT DISTRIBUTION:
{% for cpu_count, hosts in cpu_groups.items() %}
- {{ cpu_count }} CPUs: {{ hosts | length }} nodes
Hosts: {{ hosts | join(', ') }}
{% endfor %}
MEMORY SIZE DISTRIBUTION:
{% for memory_size, hosts in memory_groups.items() %}
- {{ memory_size }}GB RAM: {{ hosts | length }} nodes
Hosts: {{ hosts | join(', ') }}
{% endfor %}
```
- name: Display Detailed Hardware Information
debug:
msg: |
```
================================================
DETAILED HARDWARE INFORMATION
================================================
HOST: {{ item.hostname }}
- CPU Count: {{ item.cpu_count }}
- Memory: {{ item.memory_gb }}GB
- Disks: {{ item.disks | join(', ') }}
- Unmounted Partitions: {{ item.unmounted_partitions | join(', ') if item.unmounted_partitions else 'None' }}
```
loop: “{{ hw_specs }}”
- name: Generate CSV Report
copy:
content: |
Hostname,CPU_Count,Memory_GB,Disks,Unmounted_Partitions
{% for spec in hw_specs -%}
{{ spec.hostname }},{{ spec.cpu_count }},{{ spec.memory_gb }},”{{ spec.disks | join(’;’) }}”,”{{ spec.unmounted_partitions | join(’;’) }}”
{% endfor %}
dest: “./hardware_specs_report.csv”
- name: Display report location
debug:
msg: “Hardware specification report saved to: ./hardware_specs_report.csv”