feat: icinga ansible dumper

This commit is contained in:
2022-12-28 15:50:12 +01:00
parent 6976980b96
commit d14dbce550
4 changed files with 62 additions and 16 deletions

18
icinga.py Normal file
View File

@@ -0,0 +1,18 @@
import jinja2
environment = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath="./templates"))
def createMasterHostConfig(vmList):
template = environment.get_template("icinga_host.conf.j2")
with open("build/icinga_master_hosts.conf", "w") as f:
for vmo in vmList:
if not vmo.check:
continue
checkDomains = filter(lambda x: not x.get("nocheck"), vmo.subdomains)
websites = [ (s["name"], s.get("url")) for s in checkDomains]
f.write(template.render(hostname=vmo.hostname, address=vmo.ip, websites=websites))
def createMasterServiceConfig(vmList):
pass

View File

@@ -2,6 +2,7 @@ import json
import vm
import sys
import jinja2
import icinga
ACME_CONTENT = '''
location /.well-known/acme-challenge/ {
@@ -74,3 +75,6 @@ if __name__ == "__main__":
content = template.render(nginxJson)
f.write(content)
# dump icinga master
icinga.createMasterHostConfig(vmList)

View File

@@ -0,0 +1,31 @@
object Host "{{ hostname }}" {
import "generic-host"
name = "{{ hostname }}"
address = "{{ address }}"
vars.remote = "true"
vars.linux = "true"
max_check_attempts = 7
retry_interval = 1m
vars.notification["mail"] = {
groups = ["icingaadmins"]
}
{% for website_name, url in websites %}
vars.http_vhosts["{{ website_name }}"] = {
{% if url %}
http_uri = "{{ url }}"
{% else %}
http_uri = "/"
{% endif %}
http_address = "https://{{ website_name }}"
http_ssl = true
http_onredirect = "follow"
}
{% endfor %}
}

19
vm.py
View File

@@ -10,6 +10,7 @@ class VM:
self.hostname = args.get("hostname")
self.subdomains = args.get("subdomains")
self.ports = args.get("ports")
self.check = not args.get("nocheck")
self.terminateSSL = args.get("terminate-ssl")
self.network = args.get("network") or "default"
self.isExternal = args.get("external")
@@ -95,25 +96,17 @@ class VM:
# https components #
components = []
template = self.environment.get_template("nginx_server_block.conf.j2")
targetport = 80
if all([type(e) == dict for e in self.subdomains]):
for subdomain in self.subdomains:
if type(subdomain) != dict:
raise ValueError("Subdomain must be object containing 'name' ")
compositeName = "-".join((self.hostname, subdomain["name"].replace(".","-")))
targetport = subdomain["port"]
targetport = subdomain.get("port") or 80
component = template.render(targetip=self.ip, targetport=targetport,
servernames=[subdomain["name"]], comment=compositeName,
proxy_pass_blob=self.proxy_pass_blob, acme=not self.noTerminateACME)
components.append(component)
elif any([type(e) == dict for e in self.subdomains]):
raise ValueError("Mixed subdomains not allowed - must be all complex or all simple")
else:
compositeName = "-".join((self.hostname, self.subdomains[0].replace(".","-")))
component = template.render(targetip=self.ip, targetport=targetport,
servernames=self.subdomains, comment=compositeName,
proxy_pass_blob=self.proxy_pass_blob, acme= not self.noTerminateACME)
components.append(component)
return components