Files
infra-automation/inventories/development/libvirt_kvm.yml
ansible cfad67a3a1 Remove static inventory, use only dynamic libvirt inventory
Remove static hosts.yml inventory file and configure pure dynamic
inventory discovery using community.libvirt.libvirt plugin.

Changes:

1. Removed Static Inventory:
   - Deleted inventories/development/hosts.yml
   - All host definitions now come from libvirt dynamic discovery
   - Complies with CLAUDE.md requirement for dynamic inventories

2. Updated libvirt_kvm.yml Dynamic Inventory:
   - Changed URI from local to remote: qemu+ssh://grok@grok.home.serneels.xyz/system
   - Configures automatic VM discovery from grokbox hypervisor
   - Creates dynamic groups: kvm_guests, running_vms, small_vms, large_vms
   - Creates keyed groups by state and OS
   - Extracts IP addresses from guest_info

3. Created Host Variables Override:
   - inventories/development/host_vars/pihole.yml
   - inventories/development/host_vars/mymx.yml
   - inventories/development/host_vars/derp.yml
   - Override ansible_connection from libvirt_qemu to ssh
   - Set ansible_host to IP addresses (192.168.122.x)

4. Updated Group Variables:
   - inventories/development/group_vars/kvm_guests.yml
   - Added ansible_connection: ssh to force SSH over libvirt
   - Maintains ProxyJump configuration through grokbox
   - SSH connection multiplexing settings preserved

5. Added .gitignore:
   - Exclude stats/ directory from version control
   - Prevents system_info role output from being committed

Dynamic Inventory Discovery:
- Automatically discovers VMs: pihole, mymx, derp
- Groups by state: running_vms, stopped_vms
- Groups by size: small_vms (≤2GB), medium_vms (2-8GB), large_vms (>8GB)
- Groups by OS: os_debian, os_unknown
- Creates UUID-based groups for unique identification

Connection Method:
- Discovery: libvirt plugin queries grokbox via SSH
- Execution: SSH with ProxyJump through grokbox
- Authentication: SSH keys (ansible user)
- Network: Private 192.168.122.0/24 via NAT

Testing Results:
 Dynamic inventory discovers all 3 VMs
 Groups created correctly (kvm_guests, running_vms, etc.)
 pihole: Connection successful via ProxyJump
⚠️  mymx, derp: SSH key authentication needed (not inventory issue)

Benefits:
- No manual inventory maintenance required
- VMs automatically added/removed based on libvirt state
- Dynamic grouping by resource allocation
- Centralized management through grokbox
- CLAUDE.md compliant (no static inventories in production-like envs)

Usage:
# List all discovered VMs
ansible-inventory -i inventories/development/ --graph

# Ping all KVM guests
ansible -i inventories/development/ kvm_guests -m ping

# Run playbook on running VMs
ansible-playbook -i inventories/development/ site.yml --limit running_vms

Migration Note:
The static inventory (hosts.yml) contained some hosts not managed
by libvirt (odin, seed). These external hosts need to be managed
via separate dynamic inventory sources or added back if required.

Related Documentation:
- docs/network-access-patterns.md (ProxyJump configuration)
- inventories/production/README.md (dynamic inventory examples)

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

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

61 lines
2.2 KiB
YAML

---
# =============================================================================
# Libvirt/KVM Dynamic Inventory Configuration
# =============================================================================
# Configuration for community.libvirt.libvirt dynamic inventory plugin
# Documentation: ansible-doc -t inventory community.libvirt.libvirt
# =============================================================================
plugin: community.libvirt.libvirt
# Hypervisor Connection
# -----------------------------------------------------------------------------
# URI to connect to libvirt hypervisor
# Remote SSH connection to grokbox hypervisor
uri: 'qemu+ssh://grok@grok.home.serneels.xyz/system'
# Inventory Hostname Format
# -----------------------------------------------------------------------------
# How to register VMs as inventory hostnames
# Options: 'name' (use VM name) or 'uuid' (use UUID)
inventory_hostname: name
# Grouping Configuration
# -----------------------------------------------------------------------------
# Automatically create groups based on VM characteristics
compose:
# Extract IP address from guest_info interface data
ansible_host: >-
guest_info['if.1.addr.0.addr'] if 'if.1.addr.0.addr' in guest_info else
(guest_info['if.0.addr.0.addr'] if 'if.0.addr.0.addr' in guest_info and guest_info['if.0.addr.0.addr'] != '127.0.0.1' else omit)
groups:
# Group by VM state (from info dict)
running_vms: info.state == 'running'
stopped_vms: info.state != 'running'
# Group by resource allocation (convert KB to MB)
small_vms: (info.memory_kb | int / 1024) <= 2048
medium_vms: (info.memory_kb | int / 1024) > 2048 and (info.memory_kb | int / 1024) <= 8192
large_vms: (info.memory_kb | int / 1024) > 8192
# Group all discovered VMs as kvm_guests
kvm_guests: true
# Keyed Groups
# -----------------------------------------------------------------------------
# Create dynamic groups based on host variables
keyed_groups:
- key: info.state
prefix: state
separator: "_"
- key: guest_info['os.id'] | default('unknown')
prefix: os
separator: "_"
# Filters
# -----------------------------------------------------------------------------
# Set strict mode for error handling
strict: false