From 9461a95639178201d56359c045144e0c83cd67ca Mon Sep 17 00:00:00 2001 From: Sheppy Date: Wed, 28 Dec 2022 19:18:48 +0100 Subject: [PATCH] feat: add icinga & ansible file generation --- icinga.py | 2 +- main.py | 11 +++++++++++ vm.py | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/icinga.py b/icinga.py index 4f3e60a..deb176b 100644 --- a/icinga.py +++ b/icinga.py @@ -3,7 +3,7 @@ environment = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath="./te def createMasterHostConfig(vmList): template = environment.get_template("icinga_host.conf.j2") - with open("build/icinga_master_hosts.conf", "w") as f: + with open("ansible/files/icinga_master_hosts.conf", "w") as f: for vmo in vmList: if not vmo.check: diff --git a/main.py b/main.py index 0282547..7136a4a 100644 --- a/main.py +++ b/main.py @@ -78,3 +78,14 @@ if __name__ == "__main__": # dump icinga master icinga.createMasterHostConfig(vmList) + + # dump ansible + with open("./ansible/hosts.ini", "w") as f: + env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath="./templates")) + template = env.get_template("hosts.ini.j2") + for vmo in set(vmList): + if vmo.ansible: + f.write(template.render(hostname=vmo.hostname, ip=vmo.ip)) + f.write("\n") + + diff --git a/vm.py b/vm.py index 650110b..f8e6034 100644 --- a/vm.py +++ b/vm.py @@ -15,6 +15,7 @@ class VM: self.network = args.get("network") or "default" self.isExternal = args.get("external") self.noTerminateACME = args.get("no-terminate-acme") + self.ansible = not args.get("noansible") if self.isExternal: self.lease = None @@ -39,6 +40,13 @@ class VM: raise ValueError("Hostname {} doesn't have a DHCP lease".format(self.hostname)) + def __hash__(self): + return hash(self.hostname) + + def __eq__(self, other): + return self.hostname == other.hostname + + def dumpStreamComponents(self): # port forwarding components #