Add support for per-zone template

This commit is contained in:
Benjamin Collet 2021-04-11 10:24:41 +02:00
parent 8563b30fc9
commit 27bfe74186
3 changed files with 21 additions and 16 deletions

View file

@ -5,9 +5,12 @@ zones:
- type: 'reverse' - type: 'reverse'
name: '192.0.2.0/24' name: '192.0.2.0/24'
file: '/path/to/file/db.192.0.2' file: '/path/to/file/db.192.0.2'
template: 'zonefile.jj2'
- type: 'reverse' - type: 'reverse'
name: '2001:db8::/32' name: '2001:db8::/32'
file: '/path/to/file/db'2001:db8' file: '/path/to/file/db'2001:db8'
template: 'zonefile.jj2'
- type: 'forward' - type: 'forward'
name: 'example.com' name: 'example.com'
file: '/path/to/file/example.com' file: '/path/to/file/example.com'
template: 'zonefile.jj2'

View file

@ -15,7 +15,7 @@ config = os.path.join(os.path.dirname(os.path.realpath(__file__)),'config.yml')
with open(config, 'r') as ymlfile: with open(config, 'r') as ymlfile:
cfg = yaml.load(ymlfile, Loader=yaml.FullLoader) cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
templates_dir = os.path.dirname(os.path.realpath(__file__)) templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),'templates')
def ptr(nb, args): def ptr(nb, args):
device_primary = {} device_primary = {}
@ -81,7 +81,7 @@ def ptr(nb, args):
file_loader = FileSystemLoader(templates_dir) file_loader = FileSystemLoader(templates_dir)
env = Environment(loader=file_loader) env = Environment(loader=file_loader)
template = env.get_template("zonefile.jj2") template = env.get_template(args.template)
output = template.render(serial=serial,records=records) output = template.render(serial=serial,records=records)
if not hasattr(args, 'output'): if not hasattr(args, 'output'):
@ -166,7 +166,7 @@ def dns(nb, args):
file_loader = FileSystemLoader(templates_dir) file_loader = FileSystemLoader(templates_dir)
env = Environment(loader=file_loader) env = Environment(loader=file_loader)
template = env.get_template("zonefile.jj2") template = env.get_template(args.template)
output = template.render(serial=serial,records=records) output = template.render(serial=serial,records=records)
if not hasattr(args, 'output'): if not hasattr(args, 'output'):
@ -182,9 +182,9 @@ def autodns(nb, args):
for zone in cfg['zones']: for zone in cfg['zones']:
if zone['type'] == 'reverse': if zone['type'] == 'reverse':
ptr(nb, argparse.Namespace(prefix=zone['name'],output=zone['file'])) ptr(nb, argparse.Namespace(prefix=zone['name'],template=zone['template'],output=zone['file']))
elif zone['type'] == 'forward': elif zone['type'] == 'forward':
dns(nb, argparse.Namespace(domain=zone['name'],output=zone['file'])) dns(nb, argparse.Namespace(domain=zone['name'],template=zone['template'],output=zone['file']))
if __name__ == '__main__': if __name__ == '__main__':
@ -200,9 +200,11 @@ if __name__ == '__main__':
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')
subparser.add_argument('template', type=str, help='template')
subparser = subparsers.add_parser('dns', help='Generate DNS zone file for domain') subparser = subparsers.add_parser('dns', help='Generate DNS zone file for domain')
subparser.add_argument('domain', type=str, help='Domain') subparser.add_argument('domain', type=str, help='Domain')
subparser.add_argument('template', type=str, help='template')
args = parser.parse_args() args = parser.parse_args()
globals()[args.action](nb, args) globals()[args.action](nb, args)