Add an option to generate all DNS zone files specified in configuration

This commit is contained in:
Benjamin Collet 2021-04-10 16:30:02 +02:00
parent 7d9f05445a
commit 0451515d07
2 changed files with 36 additions and 2 deletions

View file

@ -1,3 +1,13 @@
netbox: netbox:
url: 'https://netbox.local' url: 'https://netbox.local'
token: '' token: ''
zones:
- type: 'reverse'
name: '192.0.2.0/24'
file: '/path/to/file/db.192.0.2'
- type: 'reverse'
name: '2001:db8::/32'
file: '/path/to/file/db'2001:db8'
- type: 'forward'
name: 'example.com'
file: '/path/to/file/example.com'

View file

@ -83,7 +83,13 @@ def ptr(nb, args):
env = Environment(loader=file_loader) env = Environment(loader=file_loader)
template = env.get_template("zonefile.jj2") template = env.get_template("zonefile.jj2")
output = template.render(serial=serial,records=records) output = template.render(serial=serial,records=records)
print(output)
if not hasattr(args, 'output'):
print(output)
else:
f = open(args.output,'w')
f.write(output)
f.close()
def dns(nb, args): def dns(nb, args):
devices_id = [] devices_id = []
@ -162,7 +168,23 @@ def dns(nb, args):
env = Environment(loader=file_loader) env = Environment(loader=file_loader)
template = env.get_template("zonefile.jj2") template = env.get_template("zonefile.jj2")
output = template.render(serial=serial,records=records) output = template.render(serial=serial,records=records)
print(output)
if not hasattr(args, 'output'):
print(output)
else:
f = open(args.output,'w')
f.write(output)
f.close()
def autodns(nb, args):
if not 'zones' in cfg:
return
for zone in cfg['zones']:
if zone['type'] == 'reverse':
ptr(nb, argparse.Namespace(prefix=zone['name'],output=zone['file']))
elif zone['type'] == 'forward':
dns(nb, argparse.Namespace(domain=zone['name'],output=zone['file']))
if __name__ == '__main__': if __name__ == '__main__':
@ -174,6 +196,8 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Netbox API exporter') parser = argparse.ArgumentParser(description='Netbox API exporter')
subparsers = parser.add_subparsers(help='Action to perform',dest='action',required=True) subparsers = parser.add_subparsers(help='Action to perform',dest='action',required=True)
subparser = subparsers.add_parser('autodns', help='Generate all DNS zone files using configuration')
subparser = subparsers.add_parser('ptr', help='Generate reverse DNS zone file for prefix') subparser = subparsers.add_parser('ptr', help='Generate reverse DNS zone file for prefix')
subparser.add_argument('prefix', type=str, help='Prefix') subparser.add_argument('prefix', type=str, help='Prefix')