Kapisi/bin/generate-pihole-dns-dhcp.py

83 lines
2.9 KiB
Python
Raw Normal View History

2021-12-19 21:32:19 -06:00
#!/usr/bin/env python3
# File: generate-pihole-dns-dhcp.py
#
2021-12-19 21:32:19 -06:00
# Description: This file generates the DNS and DHCP files for pihole.
# It expects that the inventory has two levels of grouping.
#
2021-12-19 21:32:19 -06:00
# Package: AniNIX/Ubiqtorate
# Copyright: WTFPL
#
2021-12-19 21:32:19 -06:00
# Author: DarkFeather <darkfeather@aninix.net>
import os
import re
import subprocess
2021-12-19 21:32:19 -06:00
import sys
from kapisi_lib import *
2021-12-19 21:32:19 -06:00
rolepath='../roles/Chappaai/files'
2022-03-25 06:08:12 -05:00
dnsfilepath=rolepath+"/dns"
dhcpfilepath=rolepath+"/dhcp"
entryset={}
2021-12-19 21:32:19 -06:00
def WriteDHCPEntries(replica_domain,dhcpfile):
2021-12-19 21:32:19 -06:00
### Create the DHCP entry
# param content: the yaml content to parse
# param hosttype: managed or unmanaged
# param hostclass: the type of host as classified in the yaml
global entryset
for host in entryset:
# Entries should be:
# dhcp-host=mac,ip,fqdn
dhcpfile.write('dhcp-host=' + entryset[host][1] + ',' + entryset[host][0] + ',' + host + '.' + replica_domain + '\n')
2021-12-19 21:32:19 -06:00
def WriteDNSEntries(replica_domain,dnsfile):
2021-12-19 21:32:19 -06:00
### Create the DNS entry
# param content: the yaml content to parse
# param hosttype: managed or unmanaged
# param hostclass: the type of host as classified in the yaml
global entryset
for host in entryset:
# Entries should be:
# ip host fqdn
dnsfile.write(entryset[host][0] + ' ' + host + '.' + replica_domain + ' ' + host + '\n')
2021-12-19 21:32:19 -06:00
def GenerateFiles(file):
### Open the file and parse it
# param file: the file to work on
global dnsfile
2022-03-25 06:08:12 -05:00
if not os.path.isdir(rolepath):
os.mkdir(rolepath)
2021-12-19 21:32:19 -06:00
# Parse the yaml
with open(file, 'r') as stream:
content = yaml.safe_load(stream)
replica_domain = content['all']['vars']['replica_domain']
external_domain = content['all']['vars']['external_domain']
2021-12-19 21:32:19 -06:00
# Clear the DNS file
with open(dhcpfilepath,'w') as dhcpfile:
dhcpfile.write('dhcp-range='+content['all']['vars']['dhcprange']+'\n')
dhcpfile.write('dhcp-option=option:dns-server,'+content['all']['vars']['dns']+'\n\n')
dhcpfile.write('dhcp-range='+content['all']['vars']['staticrange']+'\n')
WriteDHCPEntries(replica_domain,dhcpfile)
2021-12-19 21:32:19 -06:00
with open(dnsfilepath,'w') as dnsfile:
dnsfile.write(content['all']['vars']['webfront']+' '+external_domain+' '+content['all']['vars']['external_subdomains'].replace(' ','.'+external_domain+' ')+'.'+external_domain+' '+content['all']['vars']['hosted_domains']+"\n")
WriteDNSEntries(replica_domain,dnsfile)
print('Files should be in '+rolepath);
2021-12-19 21:32:19 -06:00
### Main function
# param sys.argv: Input arguments
2021-12-19 21:32:19 -06:00
if __name__ == '__main__':
if len(sys.argv) < 2:
2021-12-19 21:32:19 -06:00
print("You need to supply an inventory file.")
sys.exit(1)
if len(sys.argv) == 3:
entryset = TrackIPEntries(sys.argv[1],sys.argv[2])
else:
entryset = TrackIPEntries(sys.argv[1])
2021-12-19 21:32:19 -06:00
GenerateFiles(sys.argv[1])
#dumper.dump(entryset)
2021-12-19 21:32:19 -06:00
sys.exit(0)