Set up Ansible homelab infrastructure
- Inventory based on tailscale - Structure of the project
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
inventory/__pycache__/
|
||||
.venv/
|
||||
.ansible
|
||||
16
.zed/settings.json
Normal file
16
.zed/settings.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"lsp": {
|
||||
"ansible": {
|
||||
"settings": {
|
||||
"python": {
|
||||
"interpreterPath": ".venv/bin/python",
|
||||
},
|
||||
"validation": {
|
||||
"lint": {
|
||||
"path": ".venv/bin/ansible-lint",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
6
ansible.cfg
Normal file
6
ansible.cfg
Normal file
@@ -0,0 +1,6 @@
|
||||
[defaults]
|
||||
inventory = inventory/
|
||||
host_key_checking = False
|
||||
|
||||
[ssh_connection]
|
||||
pipelining = True
|
||||
3
group_vars/debian_hosts.yml
Normal file
3
group_vars/debian_hosts.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
ansible_user: debian
|
||||
ansible_become: true
|
||||
ansible_become_method: sudo
|
||||
1
group_vars/root_hosts.yml
Normal file
1
group_vars/root_hosts.yml
Normal file
@@ -0,0 +1 @@
|
||||
ansible_user: root
|
||||
11
homelab.yml
Normal file
11
homelab.yml
Normal file
@@ -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
|
||||
15
inventory/static_groups.yml
Normal file
15
inventory/static_groups.yml
Normal file
@@ -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:
|
||||
46
inventory/tailscale_inventory.py
Executable file
46
inventory/tailscale_inventory.py
Executable file
@@ -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, <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()
|
||||
0
roles/common/tasks/main.yml
Normal file
0
roles/common/tasks/main.yml
Normal file
0
roles/repos/tasks/main.yml
Normal file
0
roles/repos/tasks/main.yml
Normal file
4
roles/ufw/defaults/main.yml
Normal file
4
roles/ufw/defaults/main.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
# roles/ufw/defaults/main.yml
|
||||
---
|
||||
ufw_tailscale_interface: tailscale0
|
||||
ufw_extra_rules: []
|
||||
0
roles/ufw/tasks/main.yml
Normal file
0
roles/ufw/tasks/main.yml
Normal file
0
roles/upgrade/tasks/main.yml
Normal file
0
roles/upgrade/tasks/main.yml
Normal file
Reference in New Issue
Block a user