Skip to content

Commit 4b385c0

Browse files
committed
pihole 6
1 parent 9e3f699 commit 4b385c0

File tree

11 files changed

+834
-1
lines changed

11 files changed

+834
-1
lines changed

ansible/inventory/group_vars/all.yml

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ install_oh_my_zsh: true
77
push_ssh_keys: true
88

99
pihole:
10+
api_url: "https://pihole.local.ildoc.it/admin"
11+
api_key: "{{ lookup('community.hashi_vault.hashi_vault', 'ansible/data/pihole:api_key') }}"
1012
dns_records:
1113

1214
- { ip: 192.168.0.10, domain: "pve01" }
@@ -83,7 +85,102 @@ pihole:
8385
- { mac: AA:AA:AA:AA:CA:CE, ip: 192.168.0.40, hostname: redis }
8486

8587
- { mac: aa:bb:cc:11:22:88, ip: 192.168.0.41, hostname: webserver-app }
86-
- { mac: aa:bb:cc:22:11:77, ip: 192.168.0.42, hostname: mongodb-db }
88+
- { mac: aa:bb:cc:22:11:77, ip: 192.168.0.42, hostname: mongodb-db }
89+
90+
adlists:
91+
- url: "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
92+
comment: "Steven Black's Unified Hosts List"
93+
enabled: true
94+
95+
- url: "https://raw.githubusercontent.com/PolishFiltersTeam/KADhosts/master/KADhosts.txt"
96+
comment: "KAD hosts"
97+
enabled: true
98+
99+
- url: https://v.firebog.net/hosts/AdguardDNS.txt
100+
comment: "Adguard DNS"
101+
enabled: true
102+
103+
- url: "https://v.firebog.net/hosts/Easyprivacy.txt"
104+
comment: "EasyPrivacy"
105+
enabled: true
106+
107+
- url: "https://www.github.developerdan.com/hosts/lists/ads-and-tracking-extended.txt"
108+
comment: "Ads and tracking extended"
109+
enabled: true
110+
111+
- url: "https://raw.githubusercontent.com/FadeMind/hosts.extras/master/add.Spam/hosts"
112+
comment: "FadeMind List"
113+
enabled: true
114+
115+
- url: "https://v.firebog.net/hosts/static/w3kbl.txt"
116+
comment: "Firebog w3kbl list"
117+
enabled: true
118+
119+
- url: "https://v.firebog.net/hosts/Prigent-Ads.txt"
120+
comment: "Firebog Prigent ads list"
121+
enabled: true
122+
123+
- url: "https://raw.githubusercontent.com/hagezi/dns-blocklists/main/wildcard/pro.txt"
124+
comment: "Hagezi Pro Blocklist"
125+
enabled: true
126+
127+
- url: "https://raw.githubusercontent.com/Perflyst/PiHoleBlocklist/master/SmartTV-AGH.txt"
128+
comment: "Smart TV AGH Blocklist"
129+
enabled: true
130+
131+
- url: "https://raw.githubusercontent.com/AdAway/adaway.github.io/master/hosts.txt"
132+
comment: "AdAway Hosts"
133+
enabled: true
134+
135+
- url: "https://mirror1.malwaredomains.com/files/justdomains"
136+
comment: "Malware Domains"
137+
enabled: true
138+
139+
- url: "https://big.oisd.nl"
140+
comment: "OISD Full"
141+
enabled: true
142+
143+
- url: "https://someonewhocares.org/hosts/zero/hosts"
144+
comment: "Dan Pollock's List"
145+
enabled: false # Esempio di lista disabilitata
146+
147+
# Whitelist - Domini sempre permessi
148+
whitelist:
149+
domains:
150+
- domain: "clicks.eventbrite.com"
151+
comment: "Google Play Store"
152+
enabled: true
153+
154+
- domain: "click.discord.com"
155+
comment: "Google Services"
156+
enabled: true
157+
158+
- domain: "oci.external-secrets.io"
159+
comment: "YouTube"
160+
enabled: true
161+
162+
# Whitelist regex - Pattern permessi
163+
regex:
164+
- domain: "^(.+[-.])?local\\.ildoc\\.it$"
165+
comment: "All local subdomains"
166+
enabled: false
167+
168+
# Blacklist - Domini sempre bloccati
169+
blacklist:
170+
domains:
171+
- domain: "facebook.com"
172+
comment: "Block Facebook completely"
173+
enabled: false # Esempio di blocco disabilitato
174+
175+
- domain: "analytics.google.com"
176+
comment: "Google Analytics"
177+
enabled: true
178+
179+
# Blacklist regex - Pattern bloccati
180+
regex:
181+
- domain: "^(.+[-.])?tracking?\\..*$"
182+
comment: "Block tracking"
183+
enabled: false
87184

88185
nut:
89186
ups_name: ups

ansible/pihole6.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
- name: Manage dns and dhcp reservations
3+
hosts: pihole
4+
become: true
5+
gather_facts: true
6+
roles:
7+
- role: pihole6
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
- name: Ensure required variables are defined
3+
ansible.builtin.assert:
4+
that:
5+
- pihole_api_key is defined
6+
- pihole_api_url is defined
7+
fail_msg: "pihole_api_key and pihole_api_url must be defined"
8+
9+
- name: Manage DNS records via API
10+
ansible.builtin.include_tasks: manage_dns_records.yml
11+
12+
- name: Manage DHCP reservations via API
13+
ansible.builtin.include_tasks: manage_dhcp_reservations.yml
14+
15+
- name: Manage Adlists via API
16+
ansible.builtin.include_tasks: manage_adlists.yml
17+
when: pihole.adlists is defined
18+
19+
- name: Manage Whitelist domains via API
20+
ansible.builtin.include_tasks: manage_whitelist_domains.yml
21+
when: pihole.whitelist.domains is defined
22+
23+
- name: Manage Whitelist regex via API
24+
ansible.builtin.include_tasks: manage_whitelist_regex.yml
25+
when: pihole.whitelist.regex is defined
26+
27+
- name: Manage Blacklist domains via API
28+
ansible.builtin.include_tasks: manage_blacklist_domains.yml
29+
when: pihole.blacklist.domains is defined
30+
31+
- name: Manage Blacklist regex via API
32+
ansible.builtin.include_tasks: manage_blacklist_regex.yml
33+
when: pihole.blacklist.regex is defined
34+
35+
- name: Update gravity if lists changed
36+
ansible.builtin.include_tasks: update_gravity.yml
37+
when: adlists_changed is defined and adlists_changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
- name: Get existing adlists
3+
ansible.builtin.uri:
4+
url: "{{ pihole.api_url }}/api/lists/adlist"
5+
method: GET
6+
headers:
7+
X-FTL-SID: "{{ pihole.api_key }}"
8+
return_content: true
9+
register: existing_adlists
10+
11+
- name: Create lookup dict for existing adlists
12+
ansible.builtin.set_fact:
13+
existing_adlists_dict: "{{ dict(existing_adlists.json.adlists | default([]) |
14+
map(attribute='address') |
15+
zip(existing_adlists.json.adlists | default([]))) }}"
16+
17+
- name: Create lookup dict for desired adlists
18+
ansible.builtin.set_fact:
19+
desired_adlists_dict: "{{ dict(pihole.adlists |
20+
map(attribute='url') |
21+
zip(pihole.adlists)) }}"
22+
23+
- name: Get list of existing adlist URLs
24+
ansible.builtin.set_fact:
25+
existing_adlist_urls: "{{ existing_adlists.json.adlists | default([]) | map(attribute='address') | list }}"
26+
27+
- name: Get list of desired adlist URLs
28+
ansible.builtin.set_fact:
29+
desired_adlist_urls: "{{ pihole.adlists | map(attribute='url') | list }}"
30+
31+
- name: Identify adlists to add
32+
ansible.builtin.set_fact:
33+
adlists_to_add: "{{ desired_adlist_urls | difference(existing_adlist_urls) }}"
34+
35+
- name: Identify adlists to remove
36+
ansible.builtin.set_fact:
37+
adlists_to_remove: "{{ existing_adlist_urls | difference(desired_adlist_urls) }}"
38+
39+
- name: Identify adlists to update
40+
ansible.builtin.set_fact:
41+
adlists_to_update: []
42+
43+
- name: Check for adlists needing updates
44+
ansible.builtin.set_fact:
45+
adlists_to_update: "{{ adlists_to_update + [item] }}"
46+
loop: "{{ desired_adlist_urls | intersect(existing_adlist_urls) }}"
47+
when:
48+
- existing_adlists_dict[item].enabled != (desired_adlists_dict[item].enabled | default(true) | int) or
49+
existing_adlists_dict[item].comment != (desired_adlists_dict[item].comment | default(''))
50+
51+
- name: Add new adlists
52+
ansible.builtin.uri:
53+
url: "{{ pihole.api_url }}/api/lists/adlist"
54+
method: POST
55+
headers:
56+
X-FTL-SID: "{{ pihole.api_key }}"
57+
Content-Type: "application/json"
58+
body_format: json
59+
body:
60+
address: "{{ desired_adlists_dict[item].url }}"
61+
enabled: "{{ desired_adlists_dict[item].enabled | default(true) }}"
62+
comment: "{{ desired_adlists_dict[item].comment | default('') }}"
63+
status_code: [200, 201]
64+
loop: "{{ adlists_to_add }}"
65+
when: adlists_to_add | length > 0
66+
register: adlists_add_result
67+
68+
- name: Update existing adlists
69+
ansible.builtin.uri:
70+
url: "{{ pihole.api_url }}/api/lists/adlist/{{ existing_adlists_dict[item].id }}"
71+
method: PUT
72+
headers:
73+
X-FTL-SID: "{{ pihole.api_key }}"
74+
Content-Type: "application/json"
75+
body_format: json
76+
body:
77+
enabled: "{{ desired_adlists_dict[item].enabled | default(true) }}"
78+
comment: "{{ desired_adlists_dict[item].comment | default('') }}"
79+
status_code: [200, 204]
80+
loop: "{{ adlists_to_update }}"
81+
when: adlists_to_update | length > 0
82+
register: adlists_update_result
83+
84+
- name: Remove obsolete adlists
85+
ansible.builtin.uri:
86+
url: "{{ pihole.api_url }}/api/lists/adlist/{{ existing_adlists_dict[item].id }}"
87+
method: DELETE
88+
headers:
89+
X-FTL-SID: "{{ pihole.api_key }}"
90+
status_code: [200, 204]
91+
loop: "{{ adlists_to_remove }}"
92+
when: adlists_to_remove | length > 0
93+
register: adlists_remove_result
94+
95+
- name: Set flag for gravity update
96+
ansible.builtin.set_fact:
97+
adlists_changed: true
98+
when: (adlists_to_add | length > 0) or (adlists_to_update | length > 0) or (adlists_to_remove | length > 0)
99+
100+
- name: Display adlists changes summary
101+
ansible.builtin.debug:
102+
msg:
103+
- "Adlists added: {{ adlists_to_add | length }}"
104+
- "Adlists updated: {{ adlists_to_update | length }}"
105+
- "Adlists removed: {{ adlists_to_remove | length }}"
106+
when: (adlists_to_add | length > 0) or (adlists_to_update | length > 0) or (adlists_to_remove | length > 0)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
- name: Get existing blacklist domains
3+
ansible.builtin.uri:
4+
url: "{{ pihole.api_url }}/api/lists/blacklist"
5+
method: GET
6+
headers:
7+
X-FTL-SID: "{{ pihole.api_key }}"
8+
return_content: true
9+
register: existing_blacklist
10+
11+
- name: Create lookup dict for existing blacklist
12+
ansible.builtin.set_fact:
13+
existing_blacklist_dict: "{{ dict(existing_blacklist.json.blacklist | default([]) |
14+
map(attribute='domain') |
15+
zip(existing_blacklist.json.blacklist | default([]))) }}"
16+
17+
- name: Create lookup dict for desired blacklist
18+
ansible.builtin.set_fact:
19+
desired_blacklist_dict: "{{ dict(pihole.blacklist.domains |
20+
map(attribute='domain') |
21+
zip(pihole.blacklist.domains)) }}"
22+
23+
- name: Get list of existing blacklist domains
24+
ansible.builtin.set_fact:
25+
existing_blacklist_domains: "{{ existing_blacklist.json.blacklist | default([]) | map(attribute='domain') | list }}"
26+
27+
- name: Get list of desired blacklist domains
28+
ansible.builtin.set_fact:
29+
desired_blacklist_domains: "{{ pihole.blacklist.domains | map(attribute='domain') | list }}"
30+
31+
- name: Identify blacklist domains to add
32+
ansible.builtin.set_fact:
33+
blacklist_to_add: "{{ desired_blacklist_domains | difference(existing_blacklist_domains) }}"
34+
35+
- name: Identify blacklist domains to remove
36+
ansible.builtin.set_fact:
37+
blacklist_to_remove: "{{ existing_blacklist_domains | difference(desired_blacklist_domains) }}"
38+
39+
- name: Identify blacklist domains to update
40+
ansible.builtin.set_fact:
41+
blacklist_to_update: []
42+
43+
- name: Check for blacklist domains needing updates
44+
ansible.builtin.set_fact:
45+
blacklist_to_update: "{{ blacklist_to_update + [item] }}"
46+
loop: "{{ desired_blacklist_domains | intersect(existing_blacklist_domains) }}"
47+
when:
48+
- existing_blacklist_dict[item].enabled != (desired_blacklist_dict[item].enabled | default(true) | int) or
49+
existing_blacklist_dict[item].comment != (desired_blacklist_dict[item].comment | default(''))
50+
51+
- name: Add new blacklist domains
52+
ansible.builtin.uri:
53+
url: "{{ pihole.api_url }}/api/lists/blacklist"
54+
method: POST
55+
headers:
56+
X-FTL-SID: "{{ pihole.api_key }}"
57+
Content-Type: "application/json"
58+
body_format: json
59+
body:
60+
domain: "{{ desired_blacklist_dict[item].domain }}"
61+
enabled: "{{ desired_blacklist_dict[item].enabled | default(true) }}"
62+
comment: "{{ desired_blacklist_dict[item].comment | default('') }}"
63+
status_code: [200, 201]
64+
loop: "{{ blacklist_to_add }}"
65+
when: blacklist_to_add | length > 0
66+
67+
- name: Update existing blacklist domains
68+
ansible.builtin.uri:
69+
url: "{{ pihole.api_url }}/api/lists/blacklist/{{ existing_blacklist_dict[item].id }}"
70+
method: PUT
71+
headers:
72+
X-FTL-SID: "{{ pihole.api_key }}"
73+
Content-Type: "application/json"
74+
body_format: json
75+
body:
76+
enabled: "{{ desired_blacklist_dict[item].enabled | default(true) }}"
77+
comment: "{{ desired_blacklist_dict[item].comment | default('') }}"
78+
status_code: [200, 204]
79+
loop: "{{ blacklist_to_update }}"
80+
when: blacklist_to_update | length > 0
81+
82+
- name: Remove obsolete blacklist domains
83+
ansible.builtin.uri:
84+
url: "{{ pihole.api_url }}/api/lists/blacklist/{{ existing_blacklist_dict[item].id }}"
85+
method: DELETE
86+
headers:
87+
X-FTL-SID: "{{ pihole.api_key }}"
88+
status_code: [200, 204]
89+
loop: "{{ blacklist_to_remove }}"
90+
when: blacklist_to_remove | length > 0
91+
92+
- name: Display blacklist domains changes summary
93+
ansible.builtin.debug:
94+
msg:
95+
- "Blacklist domains added: {{ blacklist_to_add | length }}"
96+
- "Blacklist domains updated: {{ blacklist_to_update | length }}"
97+
- "Blacklist domains removed: {{ blacklist_to_remove | length }}"
98+
when: (blacklist_to_add | length > 0) or (blacklist_to_update | length > 0) or (blacklist_to_remove | length > 0)

0 commit comments

Comments
 (0)