Update Pi-hole DNS playbook for v6

Pi-hole v6 uses pihole.toml hosts array instead of custom.list.
Updated playbook to modify toml config directly via Python script.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
directlx 2026-02-04 09:56:31 -05:00
parent d7c7b82314
commit de76f5c6a8
2 changed files with 42 additions and 11 deletions

View File

@ -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

View File

@ -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')