extract-fw-host-vars.py: write host_vars/<host>/<short>-ipt-server.yml by default.
Without -o the script now writes the standard host_vars file (relative to the script, independent of the cwd). An existing file is only overwritten with --force; this is checked before connecting to the host. -o - prints to stdout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+32
-10
@@ -3,13 +3,17 @@
|
||||
Extract ipt-firewall configuration from a host and generate host_vars YAML.
|
||||
|
||||
Reads /etc/ipt-firewall/{interfaces,main}_ipv{4,6}.conf via SSH,
|
||||
maps all variables to Ansible fw_* names, and writes a host_vars file.
|
||||
maps all variables to Ansible fw_* names, and writes a host_vars file
|
||||
(default: host_vars/<hostname>/<short-hostname>-ipt-server.yml next to this
|
||||
script; an existing file is only overwritten with --force).
|
||||
|
||||
Usage:
|
||||
./extract-fw-host-vars.py <hostname> [--user USER] [--port PORT] [--sudo] [--from-dir DIR] [--output FILE] [--dry-run]
|
||||
./extract-fw-host-vars.py <hostname> [--user USER] [--port PORT] [--sudo] [--from-dir DIR]
|
||||
[--output FILE|-] [--force] [--dry-run]
|
||||
|
||||
Example:
|
||||
./extract-fw-host-vars.py cl-01.oopen.de
|
||||
./extract-fw-host-vars.py cl-01.oopen.de # -> host_vars/cl-01.oopen.de/cl-01-ipt-server.yml
|
||||
./extract-fw-host-vars.py cl-01.oopen.de -o - # print to stdout
|
||||
./extract-fw-host-vars.py cl-01.oopen.de --user root --dry-run
|
||||
"""
|
||||
|
||||
@@ -746,7 +750,10 @@ def main():
|
||||
parser.add_argument("hostname", help="Target hostname (must be in SSH config or known_hosts)")
|
||||
parser.add_argument("--user", "-u", default="chris", help="SSH user (default: chris)")
|
||||
parser.add_argument("--port", "-p", type=int, default=None, help="SSH port (default: 22)")
|
||||
parser.add_argument("--output", "-o", default=None, help="Output file (default: stdout)")
|
||||
parser.add_argument("--output", "-o", default=None,
|
||||
help="Output file, '-' for stdout "
|
||||
"(default: host_vars/<hostname>/<short-hostname>-ipt-server.yml)")
|
||||
parser.add_argument("--force", "-f", action="store_true", help="Overwrite an existing output file")
|
||||
parser.add_argument("--sudo", "-s", action="store_true",
|
||||
help="Read files via sudo (prompts for sudo password once)")
|
||||
parser.add_argument("--from-dir", "-d", default=None,
|
||||
@@ -770,6 +777,21 @@ def main():
|
||||
print(f"ssh {args.user}@{hostname} {cmd} {path}")
|
||||
return
|
||||
|
||||
# Determine the output file first, so an existing file aborts before any SSH
|
||||
out_path = None
|
||||
if args.output != "-":
|
||||
if args.output:
|
||||
out_path = Path(args.output)
|
||||
else:
|
||||
# Same as ${HOSTNAME%.*.*} in the docs: a.mx.oopen.de -> a.mx
|
||||
short = hostname.rsplit(".", 2)[0] if hostname.count(".") >= 2 else hostname
|
||||
out_path = Path(__file__).resolve().parent / "host_vars" / hostname / f"{short}-ipt-server.yml"
|
||||
|
||||
if out_path.exists() and not args.force:
|
||||
print(f"ERROR: {out_path} already exists — use --force to overwrite "
|
||||
"or -o - to print to stdout", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
contents = {}
|
||||
if args.from_dir:
|
||||
for key, path in files.items():
|
||||
@@ -814,13 +836,13 @@ def main():
|
||||
|
||||
yaml_text = render_yaml(hostname, host_vars, all_warnings)
|
||||
|
||||
if args.output:
|
||||
out_path = Path(args.output)
|
||||
out_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
out_path.write_text(yaml_text)
|
||||
print(f"Written to {out_path}", file=sys.stderr)
|
||||
else:
|
||||
if out_path is None:
|
||||
print(yaml_text)
|
||||
return
|
||||
|
||||
out_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
out_path.write_text(yaml_text)
|
||||
print(f"Written to {out_path}", file=sys.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user