commit cfacc4085404c4935839f4966b6c77444ac04f19 Author: Damien Date: Mon Jun 1 10:02:17 2026 +0200 Set up Ansible homelab infrastructure - Inventory based on tailscale - Structure of the project diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f615cb8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +inventory/__pycache__/ +.venv/ +.ansible diff --git a/.zed/settings.json b/.zed/settings.json new file mode 100644 index 0000000..77e2489 --- /dev/null +++ b/.zed/settings.json @@ -0,0 +1,16 @@ +{ + "lsp": { + "ansible": { + "settings": { + "python": { + "interpreterPath": ".venv/bin/python", + }, + "validation": { + "lint": { + "path": ".venv/bin/ansible-lint", + }, + }, + }, + }, + }, +} diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..654ce49 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,6 @@ +[defaults] +inventory = inventory/ +host_key_checking = False + +[ssh_connection] +pipelining = True diff --git a/group_vars/debian_hosts.yml b/group_vars/debian_hosts.yml new file mode 100644 index 0000000..9063a76 --- /dev/null +++ b/group_vars/debian_hosts.yml @@ -0,0 +1,3 @@ +ansible_user: debian +ansible_become: true +ansible_become_method: sudo diff --git a/group_vars/root_hosts.yml b/group_vars/root_hosts.yml new file mode 100644 index 0000000..a03e94e --- /dev/null +++ b/group_vars/root_hosts.yml @@ -0,0 +1 @@ +ansible_user: root diff --git a/homelab.yml b/homelab.yml new file mode 100644 index 0000000..8128b6b --- /dev/null +++ b/homelab.yml @@ -0,0 +1,11 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/playbook.json +--- +- name: Homelab alignment + hosts: online + become: true + + roles: + - role: repos + - role: common + - role: ufw + - role: upgrade diff --git a/inventory/static_groups.yml b/inventory/static_groups.yml new file mode 100644 index 0000000..22e5d7d --- /dev/null +++ b/inventory/static_groups.yml @@ -0,0 +1,15 @@ +# inventory/static_groups.yml +all: + children: + root_hosts: + hosts: + ipfabric: + pve01: + grafana: + gitea: + prometheus: + prometheus-pve-exporter: + gitea-runner: + debian_hosts: + hosts: + proxy: diff --git a/inventory/tailscale_inventory.py b/inventory/tailscale_inventory.py new file mode 100755 index 0000000..8d0a91a --- /dev/null +++ b/inventory/tailscale_inventory.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +""" +Dynamic Ansible inventory via local tailscale status. +Only includes peers tagged with tag:homelab. +Groups: online, offline, +""" + +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() diff --git a/roles/common/tasks/main.yml b/roles/common/tasks/main.yml new file mode 100644 index 0000000..e69de29 diff --git a/roles/repos/tasks/main.yml b/roles/repos/tasks/main.yml new file mode 100644 index 0000000..e69de29 diff --git a/roles/ufw/defaults/main.yml b/roles/ufw/defaults/main.yml new file mode 100644 index 0000000..bef122b --- /dev/null +++ b/roles/ufw/defaults/main.yml @@ -0,0 +1,4 @@ +# roles/ufw/defaults/main.yml +--- +ufw_tailscale_interface: tailscale0 +ufw_extra_rules: [] diff --git a/roles/ufw/tasks/main.yml b/roles/ufw/tasks/main.yml new file mode 100644 index 0000000..e69de29 diff --git a/roles/upgrade/tasks/main.yml b/roles/upgrade/tasks/main.yml new file mode 100644 index 0000000..e69de29