Files
homelab-ansible/inventory/tailscale_inventory.py
Damien cfacc40854 Set up Ansible homelab infrastructure
- Inventory based on tailscale
- Structure of the project
2026-06-01 10:02:17 +02:00

47 lines
1.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Dynamic Ansible inventory via local tailscale status.
Only includes peers tagged with tag:homelab.
Groups: online, offline, <os>
"""
import json
import subprocess
FILTER_TAG = "tag:homelab"
def run():
raw = subprocess.check_output(["tailscale", "status", "--json"])
data = json.loads(raw)
inv = {
"_meta": {"hostvars": {}},
"all": {"hosts": [], "children": ["online", "offline"]},
"online": {"hosts": []},
"offline": {"hosts": []},
}
for peer in data.get("Peer", {}).values():
# Filtre sur le tag
tags = peer.get("Tags") or []
if FILTER_TAG not in tags:
continue
name = peer["HostName"]
ip = peer["TailscaleIPs"][0]
up = peer.get("Online", False)
inv["all"]["hosts"].append(name)
inv["online" if up else "offline"]["hosts"].append(name)
inv["_meta"]["hostvars"][name] = {
"ansible_host": ip,
}
print(json.dumps(inv, indent=2))
if __name__ == "__main__":
run()