From 2583897e4c5cbe56525365206c2a64bcd0c75e5f Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 23 Nov 2023 10:49:21 +0100 Subject: wip --- 7/bgp.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 7/bgp.py (limited to '7/bgp.py') diff --git a/7/bgp.py b/7/bgp.py new file mode 100755 index 0000000..d508762 --- /dev/null +++ b/7/bgp.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +from swiplserver import PrologMQI, PrologThread +import yaml +import os + +def to_ansible(kind, hosts): + for host, o in hosts.items(): + print(f"host={host}") + os.makedirs(f"host_vars/{host}", exist_ok=True) + with open(f"host_vars/{host}/{kind}.yaml", "w") as f: + f.write(yaml.dump(o)) + +with PrologMQI() as mqi: + with mqi.create_thread() as p: + result = p.query("consult(main), main.") + print(result) + + result = p.query("bgp:bird_config(BirdDict)") + r = result[0]["BirdDict"] + print(yaml.dump(r)) + to_ansible("bgp", r) + + hosts = {} + result = p.query("firewall:fw_rule(Host, Attrs).") +# print(yaml.dump(result)) + for r in result: + host = r["Host"] + if host not in hosts: + hosts[host] = h = {} + h["firewall_rules"] = rules = [] + rules.append(r["Attrs"]) + + to_ansible("firewall", hosts) -- cgit v1.2.3