diff --git a/playbooks/pihole-dns.yml b/playbooks/pihole-dns.yml index 06eb790..affd699 100644 --- a/playbooks/pihole-dns.yml +++ b/playbooks/pihole-dns.yml @@ -1,5 +1,5 @@ --- -- name: Configure Pi-hole local DNS records +- name: Configure Pi-hole v6 local DNS records hosts: pihole vars: dns_domain: lab.directlx.dev @@ -22,16 +22,25 @@ - { ip: "192.168.200.61", hostname: "odoo" } tasks: - - name: Create Pi-hole custom DNS records + - name: Copy DNS update script ansible.builtin.template: - src: ../templates/pihole-custom-list.j2 - dest: /etc/pihole/custom.list - owner: root - group: root - mode: '0644' - notify: Restart pihole dns + src: ../templates/pihole-hosts.py.j2 + dest: /tmp/update_pihole_hosts.py + mode: '0755' + + - name: Update Pi-hole DNS hosts + ansible.builtin.command: python3 /tmp/update_pihole_hosts.py + register: update_result + changed_when: "'updated' in update_result.stdout.lower()" + notify: Restart pihole-FTL + + - name: Cleanup script + ansible.builtin.file: + path: /tmp/update_pihole_hosts.py + state: absent handlers: - - name: Restart pihole dns - ansible.builtin.command: pihole restartdns - changed_when: true + - name: Restart pihole-FTL + ansible.builtin.systemd: + name: pihole-FTL + state: restarted diff --git a/templates/pihole-hosts.py.j2 b/templates/pihole-hosts.py.j2 new file mode 100644 index 0000000..3dd6311 --- /dev/null +++ b/templates/pihole-hosts.py.j2 @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +# Update Pi-hole v6 hosts in pihole.toml +import re + +hosts = ''' +{% for record in dns_records %} + "{{ record.ip }} {{ record.hostname }}.{{ dns_domain }} {{ record.hostname }}", +{% endfor %} +''' + +with open('/etc/pihole/pihole.toml', 'r') as f: + content = f.read() + +# Find and replace hosts array +pattern = r'hosts = \[.*?\]' +replacement = f'hosts = [{hosts} ]' +content = re.sub(pattern, replacement, content, flags=re.DOTALL) + +with open('/etc/pihole/pihole.toml', 'w') as f: + f.write(content) + +print('Pi-hole DNS hosts updated')