feat: initial import of RailianceHosts starter
This commit is contained in:
commit
9860735f82
20 changed files with 355 additions and 0 deletions
34
ansible/inventory_from_yaml.py
Normal file
34
ansible/inventory_from_yaml.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#!/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()
|
||||
hosts = {}
|
||||
for s in server_list:
|
||||
name = s['name']
|
||||
hosts[name] = {
|
||||
"ansible_host": tf.get(name) or s.get('ip'),
|
||||
"ansible_user": s.get('ssh_user', 'admin')
|
||||
}
|
||||
inv = {"all": {"hosts": hosts}}
|
||||
print(json.dumps(inv))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue