Files
railiance-infra/ansible/inventory_from_yaml.py
Bernd Worsch 679d0d67b1 feat: bootstrap and harden Railiance01 at HostEurope
- Extend base role with fail2ban, UFW k3s/Flannel rules, HISTCONTROL
- Add handlers dir for fail2ban restart
- Fix inventory script to emit correct dynamic inventory JSON format
- Add roles_path to ansible.cfg so playbook finds roles
- Add Railiance01 (92.205.62.239) to inventory/servers.yaml
- Mark workplan T03/T04/T05 as done

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-08 22:53:38 +00:00

40 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
import json, yaml, subprocess, os, sys, pathlib
def load_servers():
with open(os.path.join(os.path.dirname(__file__), '..', 'inventory', 'servers.yaml')) as f:
data = yaml.safe_load(f)
servers = data.get('servers', [])
return servers
def load_tf_outputs():
# Try to read terraform outputs to attach IPs, if available.
try:
out = subprocess.check_output(['terraform', '-chdir=../terraform/hetzner', 'output', '-json'], stderr=subprocess.DEVNULL, text=True)
j = json.loads(out)
servers = j.get('servers', {}).get('value', {})
return servers # {name: ip}
except Exception:
return {}
def main():
server_list = load_servers()
tf = load_tf_outputs()
host_names = []
hostvars = {}
for s in server_list:
name = s['name']
host_names.append(name)
hostvars[name] = {
"ansible_host": tf.get(name) or s.get('ip'),
"ansible_user": s.get('ssh_user', 'admin')
}
inv = {
"all": {"hosts": host_names},
"_meta": {"hostvars": hostvars}
}
print(json.dumps(inv))
if __name__ == "__main__":
main()