feat(slice-6): implement config loader, CLI check mode, comprehensive documentation, and shell completions

This commit is contained in:
soda64 committed 2026-10-03 09:06:51 +02:00
1 parent 869fd241e7
commit e5784db650
9 files changed
+616 -29

No files matched your search

+1 -1
View File
@@ -11,7 +11,7 @@
- [x] Slice 3 — Proxmox VE REST API collector with token auth and guest agent IP resolution
- [x] Slice 4 — In-memory deduplication and conflict resolution engine with stderr warnings
- [x] Slice 5 — Extended exporters (linux-connector, Generic YAML, XML) and --sync flag
- [ ] Slice 6 — Configuration file (config.yaml), CLI check mode (--check), and documentation
- [x] Slice 6 — Configuration file (config.yaml), CLI check mode (--check), and documentation
## Notes for a fresh session
- Project: Universal homelab and fleet inventory collector and deduplicator.
+135
View File
@@ -0,0 +1,135 @@
# Inventory Collector User Guide
`inventory-collector` is a fast, dependency-light CLI utility that aggregates infrastructure hosts from multiple homelab sources (Proxmox VE REST API, static YAML and INI files), merges and deduplicates them in memory, and generates standardized inventory files for **Ansible** and **linux-connector**.
---
## 1. Quick Start
### Basic Invocation
Running `inventory-collector` without arguments outputs standard Ansible YAML inventory directly to stdout:
```bash
inventory-collector
```
### Dry-Run Inspection (`--check`)
Preview all discovered hosts, groups, and deduplication sources in an ASCII table without modifying any files:
```bash
inventory-collector --check
```
Example output:
```text
[INFO] Discovered 12 hosts across 5 groups.
NAME IP ADDRESS SOURCE GROUPS
node0 192.168.0.101 proxmox:node proxmox_nodes
k3s-master-0 192.168.5.70 hybrid k3s_cluster, k3s_master, proxmox_vms
tool-server-1 192.168.5.61 proxmox:qemu proxmox_vms, tool_servers
synology-nas 192.168.0.2 static:hosts.ini external, storage
```
---
## 2. CLI Options Reference
| Flag | Argument | Description |
|---|---|---|
| `-o`, `--output` | `PATH` | Write inventory to destination file. Format is inferred from extension (`.xml`, `.ini`, `.json`, `.yaml`). |
| `-f`, `--format` | `FORMAT` | Explicit output format: `ansible-yaml` (default), `ansible-json`, `linux-connector`, `yaml`, `xml`, `ini`. |
| `-s`, `--source` | `PATH` | Path to static file (`.ini` or `.yaml`). Can be repeated multiple times. |
| `--list` | *none* | Ansible Dynamic Inventory script mode (outputs JSON with `_meta.hostvars` to stdout). |
| `--check`, `--dry-run` | *none* | Print summary table to stdout and exit. |
| `--sync` | *none* | Update both Ansible (`inventory/hosts.yaml`) and `linux-connector` (`~/.config/linux-connector/hosts.yaml`) simultaneously. |
| `-c`, `--config` | `PATH` | Path to configuration file. Default: `~/.config/inventory-collector/config.yaml`. |
| `-V`, `--version` | *none* | Show version and exit. |
| `-h`, `--help` | *none* | Show help message. |
---
## 3. Configuration File (`config.yaml`)
Configuration can be stored in:
- `./inventory-collector.yaml` (current directory)
- `~/.config/inventory-collector/config.yaml` (user-level)
- `/etc/inventory-collector/config.yaml` (system-level)
Environment variables `${VAR}` are automatically interpolated:
```yaml
sources:
proxmox:
enabled: true
url: "${PROXMOX_URL}"
token_id: "${PROXMOX_TOKEN_ID}"
token_secret: "${PROXMOX_TOKEN_SECRET}"
verify_ssl: false
timeout: 10
static_files:
- path: "~/.config/inventory-collector/static_hosts.ini"
required: false
defaults:
ssh_user: "lain"
ssh_port: 22
conflict_policy: "runtime_over_static"
destinations:
ansible: "inventory/hosts.yaml"
linux_connector: "~/.config/linux-connector/hosts.yaml"
```
---
## 4. Integration Examples
### Ansible CLI
Use generated static file:
```bash
ansible-playbook -i inventory/hosts.yaml site.yml
```
Or execute `inventory-collector` directly as a dynamic inventory script:
```bash
ansible-playbook -i /usr/local/bin/inventory-collector site.yml
```
### `linux-connector`
Sync directly to `linux-connector`:
```bash
inventory-collector --format linux-connector -o ~/.config/linux-connector/hosts.yaml
```
Or run simultaneous sync:
```bash
inventory-collector --sync
```
### Gitea Actions CI/CD Pipeline
In `.gitea/workflows/run-ansible.yml`:
```yaml
- name: Generate Unified Inventory
env:
ANSIBLE_PROXMOX_URL: ${{ secrets.ANSIBLE_PROXMOX_URL }}
ANSIBLE_PROXMOX_TOKEN_ID: ${{ secrets.ANSIBLE_PROXMOX_TOKEN_ID }}
ANSIBLE_PROXMOX_TOKEN_SECRET: ${{ secrets.ANSIBLE_PROXMOX_TOKEN_SECRET }}
run: |
inventory-collector --source ansible/inventory/static_hosts.ini -o ansible/inventory/hosts.yaml
```
---
## 5. Deduplication & Conflict Warnings
When the same machine is defined in multiple sources (e.g. in a static INI and dynamically discovered via Proxmox):
- **Groups & Tags:** Merged into a unified set.
- **Source:** Marked as `hybrid`.
- **IP Discrepancy:** The Proxmox runtime IP takes precedence (`runtime_over_static`), and a warning is logged to `stderr`:
```text
[WARN] IP discrepancy for host 'tool-server-1': static specifies '192.168.5.61', Proxmox runtime reports '192.168.5.99'. Using runtime IP.
```
- **IP Collisions:** If two different hostnames share the same IP, both are preserved with a collision warning:
```text
[WARN] IP collision: hosts 'nas', 'synology-nas' share the same IP '192.168.0.2'. Keeping both entries.
```
- **Stdio Cleanliness:** Standard output (`stdout`) contains only clean inventory data (JSON, YAML, XML). All warnings and progress messages go to `stderr`.