forked from mullvad/dns-blocklists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaybook.yml
213 lines (189 loc) · 7.59 KB
/
playbook.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Build all the DNS ad-block files for DoH and relay blocking
# example usage:
# $ ansible-playbook -i inventory/ playbook.yml
---
- name: Build DNS ad-block files
hosts: all
connection: local
gather_facts: false
become: false
tasks:
- name: Generate README
ansible.builtin.template:
src: templates/README.md.j2
dest: README.md
tags: readme
- name: Ensure output files are cleared
ansible.builtin.file:
dest: "output/{{ item.0 }}"
state: "{{ item.1 }}"
loop: "{{ group_names | product(['absent', 'directory']) | list }}"
- name: Ensure files are present locally
ansible.builtin.file:
state: "{{ item.1 }}"
path: "lists/{{ item.0 }}/{{ item.2.type }}"
with_nested:
- "{{ group_names }}"
- ['absent', 'directory']
- "{{ dns_adblock_lists }}"
loop_control:
label: "{{ item.0}}: {{ item.1 }}"
- name: Download latest lists
ansible.builtin.get_url:
url: "{{ item.1.url }}"
dest: "lists/{{ item.0 }}/{{ item.1.type }}/{{ item.1.name }}"
loop: "{{ group_names | product(dns_adblock_lists) | list }}"
loop_control:
label: "{{ item.1.name }}: {{ item.1.url }}"
register: _dns_adblock_download_lists
retries: 5
delay: 10
until: _dns_adblock_download_lists is succeeded
- name: Replace localhost and IP addresses in files
ansible.builtin.replace:
path: "lists/{{ item.0 }}/{{ item.1.type }}/{{ item.1.name }}"
regexp: "{{ item.2 }}"
replace: ''
with_nested:
- "{{ group_names }}"
- "{{ dns_adblock_lists }}"
- "{{ dns_replace_address }}"
loop_control:
label: "{{ item.1.name }}: {{ item.1.url }}"
- name: Remove unwanted lines containing ip6-
ansible.builtin.lineinfile:
path: "lists/{{ item.0 }}/{{ item.1.type }}/{{ item.1.name }}"
regexp: ' ip6-'
state: absent
loop: "{{ group_names | product(dns_adblock_lists) | list }}"
loop_control:
label: "{{ item.1.name }}: {{ item.1.url }}"
- name: Remove custom hosts IP addresses in files
ansible.builtin.lineinfile:
path: "lists/{{ item.0 }}/{{ item.1.type }}/{{ item.1.name }}"
regexp: '([0-9]{1,3}\.){3}[0-9]{1,3}'
state: absent
loop: "{{ group_names | product(dns_adblock_lists) | list }}"
loop_control:
label: "{{ item.1.name }}: {{ item.1.url }}"
- name: Remove custom hosts localhost in files
ansible.builtin.lineinfile:
path: "lists/{{ item.0 }}/{{ item.1.type }}/{{ item.1.name }}"
regexp: ' localhost'
state: absent
loop: "{{ group_names | product(dns_adblock_lists) | list }}"
loop_control:
label: "{{ item.1.name }}: {{ item.1.url }}"
- name: Remove comments from files
ansible.builtin.lineinfile:
path: "lists/{{ item.0 }}/{{ item.1.type }}/{{ item.1.name }}"
regexp: '#'
state: absent
loop: "{{ group_names | product(dns_adblock_lists) | list }}"
loop_control:
label: "{{ item.1.name }}: {{ item.1.url }}"
- name: Create custom file with contents
ansible.builtin.copy:
content: "{{ item.1.content }}"
dest: "lists/{{ item.0 }}/{{ item.1.type }}/custom"
force: true
loop: "{{ group_names | product(dns_adblock_lists_custom) | list }}"
when: dns_adblock_lists_custom | length
loop_control:
label: "{{ item.1.type }}/custom"
- name: Combine files into output
ansible.builtin.assemble:
src: "lists/{{ item.0 }}/{{ item.1.type }}"
dest: "output/{{ item.0 }}/{{ item.0 }}_{{ item.1.type }}.txt"
loop: "{{ group_names | product(dns_adblock_lists) | list }}"
- name: Remove whitespace at start of lines
ansible.builtin.replace:
path: "output/{{ item.0 }}/{{ item.0 }}_{{ item.1.type }}.txt"
regexp: '^ '
loop: "{{ group_names | product(dns_adblock_lists) | list }}"
loop_control:
label: "{{ item.1.name }}"
# Prevent out of zone errors caused by trailing periods
- name: Remove full stops at the end of lines
ansible.builtin.replace:
path: "output/{{ item.0 }}/{{ item.0 }}_{{ item.1.type }}.txt"
regexp: '\.$'
loop: "{{ group_names | product(dns_adblock_lists) | list }}"
loop_control:
label: "{{ item.1.name }}"
- name: Replace whitespace with new line
ansible.builtin.replace:
path: "output/{{ item.0 }}/{{ item.0 }}_{{ item.1.type }}.txt"
regexp: ' '
replace: '\n'
loop: "{{ group_names | product(dns_adblock_lists) | list }}"
loop_control:
label: "{{ item.1.name }}"
- name: Remove empty lines
ansible.builtin.lineinfile:
path: "output/{{ item.0 }}/{{ item.0 }}_{{ item.1.type }}.txt"
regexp: '^\n'
state: absent
loop: "{{ group_names | product(dns_adblock_lists) | list }}"
loop_control:
label: "{{ item.1.name }}"
- name: Build zonefiles
hosts: all
become: false
tags: zonefile
connection: local
gather_facts: false
tasks:
- run_once: true
delegate_to: localhost
block:
- name: Ensure bind-utils is installed
ansible.builtin.dnf:
name: bind-utils
state: present
become_user: root
become: true
- name: Find output files
ansible.builtin.find:
paths: ./output
recurse: true
use_regex: true
patterns: ".*.txt$" # Find raw output files
excludes: ".*.zone$" # Ignore existing zonefiles
register: _output_files
- name: Read output files
ansible.builtin.slurp:
path: "{{ item }}"
register: _output_files_slurp
loop: "{{ _output_files.files | map(attribute='path') }}"
- name: Generate zone files
ansible.builtin.copy:
content: |
$TTL 3600
;namn ttl adr- entry- origin
; klass typ
;------------------------------------------------------------------------------
@ IN SOA rpz.mullvad.net. support.mullvad.net. (
{{ now(fmt='%s') }} ; serial
10800 ; refresh
3600 ; retry
604800 ; expire
3600 ) ; ttl
IN NS localhost.
;------------------------------------------------------------------------------
{% for domain in _file_content.split('\n') %}
{% if domain | length %}
{{ domain }} IN CNAME .
{% endif %}
{% endfor %}
dest: "{{ _output_path }}"
validate: named-checkzone _ %s
vars:
_filename_full: "{{ item.source.split('/')[-1] }}" # relay_adblock.txt
_filename_short: "{{ _filename_full.split('.') | first }}" # relay_adblock
_directory: "{{ item.source.split('/')[:-1] | join('/') }}" # output/relay
_output_path: "{{ _directory }}/{{ _filename_short }}.zone" # output/relay/relay_adblock.zone
_file_content: "{{ item.content | b64decode }}"
loop: "{{ _output_files_slurp.results }}"
loop_control:
label: "{{ _output_path }}"