Skip to content

Commit 07c9270

Browse files
committed
pihole 6
1 parent 76a6fcf commit 07c9270

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

ansible/roles/pihole6/tasks/main.yml

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
# DNS Records (gestione speciale, non ha ID)
1212
- name: Manage DNS records via API
1313
ansible.builtin.include_tasks: manage_dns_records.yml
14+
tags: ['dns']
1415

1516
# DHCP Reservations (gestione speciale per MAC address)
1617
- name: Manage DHCP reservations via API
1718
ansible.builtin.include_tasks: manage_dhcp_reservations.yml
19+
tags: ['dhcp']
1820

1921
# Adlists
2022
- name: Manage Adlists via API
@@ -26,14 +28,8 @@
2628
key_attribute: "address"
2729
desired_resources: "{{ pihole.adlists }}"
2830
trigger_gravity: true
29-
update_condition: >-
30-
existing_dict[item].enabled != (desired_dict[item].enabled | default(true) | int) or
31-
existing_dict[item].comment != (desired_dict[item].comment | default(''))
32-
body_template:
33-
address: "{{ desired_dict[item].url }}"
34-
enabled: "{{ desired_dict[item].enabled | default(true) }}"
35-
comment: "{{ desired_dict[item].comment | default('') }}"
3631
when: pihole.adlists is defined
32+
tags: ['adlists', 'lists']
3733

3834
# Whitelist Domains
3935
- name: Manage Whitelist domains via API
@@ -44,14 +40,8 @@
4440
response_key: "whitelist"
4541
key_attribute: "domain"
4642
desired_resources: "{{ pihole.whitelist.domains }}"
47-
update_condition: >-
48-
existing_dict[item].enabled != (desired_dict[item].enabled | default(true) | int) or
49-
existing_dict[item].comment != (desired_dict[item].comment | default(''))
50-
body_template:
51-
domain: "{{ desired_dict[item].domain }}"
52-
enabled: "{{ desired_dict[item].enabled | default(true) }}"
53-
comment: "{{ desired_dict[item].comment | default('') }}"
5443
when: pihole.whitelist.domains is defined
44+
tags: ['whitelist', 'lists']
5545

5646
# Whitelist Regex
5747
- name: Manage Whitelist regex via API
@@ -62,14 +52,8 @@
6252
response_key: "regex"
6353
key_attribute: "domain"
6454
desired_resources: "{{ pihole.whitelist.regex }}"
65-
update_condition: >-
66-
existing_dict[item].enabled != (desired_dict[item].enabled | default(true) | int) or
67-
existing_dict[item].comment != (desired_dict[item].comment | default(''))
68-
body_template:
69-
domain: "{{ desired_dict[item].domain }}"
70-
enabled: "{{ desired_dict[item].enabled | default(true) }}"
71-
comment: "{{ desired_dict[item].comment | default('') }}"
7255
when: pihole.whitelist.regex is defined
56+
tags: ['whitelist', 'lists']
7357

7458
# Blacklist Domains
7559
- name: Manage Blacklist domains via API
@@ -80,14 +64,8 @@
8064
response_key: "blacklist"
8165
key_attribute: "domain"
8266
desired_resources: "{{ pihole.blacklist.domains }}"
83-
update_condition: >-
84-
existing_dict[item].enabled != (desired_dict[item].enabled | default(true) | int) or
85-
existing_dict[item].comment != (desired_dict[item].comment | default(''))
86-
body_template:
87-
domain: "{{ desired_dict[item].domain }}"
88-
enabled: "{{ desired_dict[item].enabled | default(true) }}"
89-
comment: "{{ desired_dict[item].comment | default('') }}"
9067
when: pihole.blacklist.domains is defined
68+
tags: ['blacklist', 'lists']
9169

9270
# Blacklist Regex
9371
- name: Manage Blacklist regex via API
@@ -98,19 +76,15 @@
9876
response_key: "regex"
9977
key_attribute: "domain"
10078
desired_resources: "{{ pihole.blacklist.regex }}"
101-
update_condition: >-
102-
existing_dict[item].enabled != (desired_dict[item].enabled | default(true) | int) or
103-
existing_dict[item].comment != (desired_dict[item].comment | default(''))
104-
body_template:
105-
domain: "{{ desired_dict[item].domain }}"
106-
enabled: "{{ desired_dict[item].enabled | default(true) }}"
107-
comment: "{{ desired_dict[item].comment | default('') }}"
10879
when: pihole.blacklist.regex is defined
80+
tags: ['blacklist', 'lists']
10981

11082
- name: Update gravity if lists changed
11183
ansible.builtin.include_tasks: update_gravity.yml
11284
when: adlists_changed is defined and adlists_changed
85+
tags: ['gravity', 'lists']
11386

11487
- name: Check and update Pi-hole
11588
ansible.builtin.include_tasks: update_pihole.yml
11689
when: pihole.check_updates | default(true)
90+
tags: ['updates']

ansible/roles/pihole6/tasks/manage_pihole_resource.yml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
ansible.builtin.set_fact:
4848
to_update: "{{ to_update + [item] }}"
4949
loop: "{{ desired_keys | intersect(existing_keys) }}"
50-
when: update_condition
50+
when: >-
51+
existing_dict[item].enabled != (desired_dict[item].enabled | default(true) | int) or
52+
existing_dict[item].comment != (desired_dict[item].comment | default(''))
5153
5254
- name: "Add new {{ resource_name }}"
5355
ansible.builtin.uri:
@@ -62,7 +64,20 @@
6264
loop: "{{ to_add }}"
6365
when: to_add | length > 0
6466
vars:
65-
add_body: "{{ body_template | combine({'item': item}) }}"
67+
add_body: >-
68+
{%- if resource_name == 'adlists' -%}
69+
{
70+
"address": "{{ desired_dict[item].url }}",
71+
"enabled": {{ desired_dict[item].enabled | default(true) }},
72+
"comment": "{{ desired_dict[item].comment | default('') }}"
73+
}
74+
{%- else -%}
75+
{
76+
"domain": "{{ desired_dict[item].domain }}",
77+
"enabled": {{ desired_dict[item].enabled | default(true) }},
78+
"comment": "{{ desired_dict[item].comment | default('') }}"
79+
}
80+
{%- endif -%}
6681
6782
- name: "Update existing {{ resource_name }}"
6883
ansible.builtin.uri:
@@ -72,7 +87,9 @@
7287
X-FTL-SID: "{{ pihole.api_key }}"
7388
Content-Type: "application/json"
7489
body_format: json
75-
body: "{{ update_body }}"
90+
body:
91+
enabled: "{{ desired_dict[item].enabled | default(true) }}"
92+
comment: "{{ desired_dict[item].comment | default('') }}"
7693
status_code: [200, 204]
7794
loop: "{{ to_update }}"
7895
when: to_update | length > 0

0 commit comments

Comments
 (0)