#!/usr/bin/env python3 import re hosts = [ {% for h in pihole_dns_hosts %} "{{ h.ip }} {{ h.host }}", {% endfor %} ] hosts_str = "[\n" + "".join(' "' + h + '",\n' for h in hosts) + " ]" with open("/etc/pihole/pihole.toml", "r") as f: content = f.read() # Only replace the first hosts= (DNS custom records section, not DHCP leases) content = re.sub( r"( *hosts\s*=\s*)\[.*?\]", r"\g<1>" + hosts_str, content, count=1, flags=re.DOTALL, ) with open("/etc/pihole/pihole.toml", "w") as f: f.write(content)