fix: broken inlining of python block moved to script
This commit is contained in:
45
scripts/new_host.py
Normal file
45
scripts/new_host.py
Normal file
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse, os, sys
|
||||
try:
|
||||
import yaml
|
||||
except Exception as e:
|
||||
print("ERROR: PyYAML is required. Install with: pip install pyyaml", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser(description="Add a host to inventory/servers.yaml")
|
||||
p.add_argument("--name", required=True)
|
||||
p.add_argument("--type", default="cpx21")
|
||||
p.add_argument("--region", default="nbg1")
|
||||
p.add_argument("--role", default="generic")
|
||||
p.add_argument("--image", default="ubuntu-24.04")
|
||||
p.add_argument("--user", default="admin")
|
||||
args = p.parse_args()
|
||||
|
||||
inv_path = os.path.join("inventory", "servers.yaml")
|
||||
data = {}
|
||||
if os.path.exists(inv_path):
|
||||
with open(inv_path, "r", encoding="utf-8") as f:
|
||||
data = yaml.safe_load(f) or {}
|
||||
servers = data.setdefault("servers", [])
|
||||
|
||||
# Prevent duplicates
|
||||
if any(s.get("name") == args.name for s in servers):
|
||||
print(f"ERROR: host '{args.name}' already exists in {inv_path}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
servers.append({
|
||||
"name": args.name,
|
||||
"labels": [],
|
||||
"role": args.role,
|
||||
"region": args.region,
|
||||
"type": args.type,
|
||||
"image": args.image,
|
||||
"ssh_user": args.user,
|
||||
})
|
||||
|
||||
with open(inv_path, "w", encoding="utf-8") as f:
|
||||
yaml.safe_dump(data, f, sort_keys=False)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user