homelab/ansible/playbooks/gather-node-facts.yml

141 lines
5.0 KiB
YAML

---
# Gather Node Facts Playbook
# Purpose: Collect accurate system information from nodes for inventory
# Usage: ansible-playbook playbooks/gather-node-facts.yml
# Add --limit <hostname> to target specific nodes
# Use -k flag only if nodes aren't onboarded yet
- name: Gather facts from managed nodes
hosts: all
gather_facts: true
become: false
vars:
output_dir: "{{ playbook_dir }}/../inventory/host_vars"
tasks:
- name: Display discovered facts summary
ansible.builtin.debug:
msg:
- "======================================"
- "Host: {{ inventory_hostname }}"
- "======================================"
- "FQDN: {{ ansible_fqdn }}"
- "Distribution: {{ ansible_distribution }} {{ ansible_distribution_version }}"
- "Kernel: {{ ansible_kernel }}"
- "Architecture: {{ ansible_architecture }}"
- "CPU Model: {{ ansible_processor[2] | default('N/A') }}"
- "CPU Cores: {{ ansible_processor_vcpus }}"
- "Memory: {{ (ansible_memtotal_mb / 1024) | round(0) }} GB"
- "Primary IP: {{ ansible_default_ipv4.address }}"
- "Hostname: {{ ansible_hostname }}"
- name: Check for GPU devices
ansible.builtin.stat:
path: /dev/dri
register: gpu_check
- name: Detect GPU information (if available)
ansible.builtin.shell: |
if command -v lspci &> /dev/null; then
lspci | grep -i vga | head -1
else
echo "lspci not available"
fi
register: gpu_info
changed_when: false
failed_when: false
when: gpu_check.stat.exists
- name: Check Docker installation
ansible.builtin.command: docker --version
register: docker_version
changed_when: false
failed_when: false
- name: Check NFS mounts
ansible.builtin.shell: mount | grep nfs || echo "No NFS mounts"
register: nfs_mounts
changed_when: false
failed_when: false
- name: Detect running Docker containers
ansible.builtin.command: docker ps --format "{{ '{{' }}.Names{{ '}}' }}"
register: docker_containers
changed_when: false
failed_when: false
when: docker_version.rc == 0
- name: Generate host_vars content
ansible.builtin.set_fact:
host_vars_content: |
---
# Host-specific variables for {{ inventory_hostname }}
# IP: {{ ansible_host }}
# Auto-generated: {{ ansible_date_time.iso8601 }}
# Hardware Details
hardware:
platform: {{ 'proxmox_vm' if 'pve' in ansible_system_vendor | lower else 'physical_server' }}
cpu: {{ ansible_processor[2] if ansible_processor | length > 2 else ansible_processor[0] }} ({{ ansible_processor_vcpus }} cores)
memory_gb: {{ (ansible_memtotal_mb / 1024) | round(0) | int }}
storage_gb: {{ (ansible_devices[ansible_devices.keys() | list | first].size | replace('GB', '') | float) | round(0) | int if ansible_devices else 'unknown' }}
architecture: {{ ansible_architecture }}
# Operating System
os:
distribution: {{ ansible_distribution }}
version: "{{ ansible_distribution_version }}"
codename: "{{ ansible_distribution_release | title }}"
kernel: {{ ansible_kernel }}
{% if gpu_check.stat.exists and gpu_info.stdout != "lspci not available" %}
# GPU Configuration
gpu:
enabled: true
device: /dev/dri
info: "{{ gpu_info.stdout }}"
{% endif %}
# Docker Status
docker:
installed: {{ docker_version.rc == 0 }}
{% if docker_version.rc == 0 %}
version: "{{ docker_version.stdout }}"
{% endif %}
{% if docker_containers.stdout_lines | default([]) | length > 0 %}
running_containers:
{% for container in docker_containers.stdout_lines %}
- {{ container }}
{% endfor %}
{% endif %}
# NFS Configuration
nfs:
mounts_configured: {{ 'nfs' in nfs_mounts.stdout }}
{% if 'nfs' in nfs_mounts.stdout %}
mount_details: |
{{ nfs_mounts.stdout | indent(6) }}
{% endif %}
# Network Configuration
network:
primary_ip: {{ ansible_default_ipv4.address }}
primary_interface: {{ ansible_default_ipv4.interface }}
hostname: {{ ansible_hostname }}
fqdn: {{ ansible_fqdn }}
- name: Display generated host_vars
ansible.builtin.debug:
msg: "{{ host_vars_content }}"
- name: Save host_vars to file (local action)
delegate_to: localhost
ansible.builtin.copy:
content: "{{ host_vars_content }}"
dest: "{{ output_dir }}/{{ inventory_hostname }}.yml"
mode: "0644"
become: false
- name: Summary
ansible.builtin.debug:
msg: "✅ Generated {{ output_dir }}/{{ inventory_hostname }}.yml"