-
Notifications
You must be signed in to change notification settings - Fork 51
/
playbook.yml
252 lines (228 loc) · 8.48 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# Build all the DNS blocklist files for DoH and relay blocking
# example usage:
# $ ansible-playbook -i inventory/ playbook.yml
---
- name: Build DNS blocklist 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: Install dos2unix and bind-utils
ansible.builtin.dnf:
name:
- dos2unix
- bind-utils
state: present
become_user: root
become: true
run_once: true
- 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_blocklists_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_blocklists_lists) | list }}"
loop_control:
label: "{{ item.1.name }}"
register: _dns_blocklists_download_lists
retries: 5
delay: 10
until: _dns_blocklists_download_lists is succeeded
when:
- item.1.url is defined
- not item.1.content is defined
- name: Create file with custom list content if defined
ansible.builtin.copy:
content: "{{ item.1.content }}"
dest: lists/{{ item.0 }}/{{ item.1.type }}/{{ item.1.name }}
loop: "{{ group_names | product(dns_blocklists_lists) | list }}"
loop_control:
label: "{{ item.1.name }}"
when:
- item.1.content is defined
- not item.1.url is defined
- name: Run dos2unix on all lists
ansible.builtin.command:
cmd: dos2unix lists/{{ item.0 }}/{{ item.1.type }}/{{ item.1.name }}
loop: "{{ group_names | product(dns_blocklists_lists) | list }}"
loop_control:
label: "{{ item.1.name }}"
- 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_blocklists_lists }}"
- "{{ _dns_blocklists_replace_address }}"
vars:
# Often blocklists contain these IPs. We want to remove them from our lists
_dns_blocklists_replace_address:
- '([0]{1,3}\.){3}[0]{1,3} ' # Beware of the necessary trailing whitespace!
- 'localhost ' # It is there to not match records like 1.2.3.4.domain.tld
- "127.0.0.1"
- '::1'
loop_control:
label: "{{ item.1.name }}"
- 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_blocklists_lists) | list }}"
loop_control:
label: "{{ item.1.name }}"
- name: Remove unused lines from files
ansible.builtin.lineinfile:
path: lists/{{ item.0 }}/{{ item.1.type }}/{{ item.1.name }}
regexp: '^{{ item.2 }}'
state: absent
with_nested:
- "{{ group_names }}"
- "{{ dns_blocklists_lists }}"
- "{{ _comments }}"
vars:
_comments:
- "#"
- ";"
- "$"
- "@"
- '\$TTL'
loop_control:
label: "{{ item.1.name }}"
- name: Replace comments with ' ' from all lines
ansible.builtin.replace:
path: lists/{{ item.0 }}/{{ item.1.type }}/{{ item.1.name }}
regexp: '#.*$'
replace: ''
loop: "{{ group_names | product(dns_blocklists_lists) | list }}"
- 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_blocklists_lists) | list }}"
loop_control:
label: "{{ item.1.name }}"
- 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_blocklists_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: '{{ item.2 }}'
with_nested:
- "{{ group_names }}"
- "{{ dns_blocklists_lists }}"
- "{{ _eol }}"
vars:
_eol:
- 'CNAME .$'
- '\.$'
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_blocklists_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_blocklists_lists) | list }}"
loop_control:
label: "{{ item.1.name }}"
- name: Ensure excluded URLS are not included in any output file
ansible.builtin.lineinfile:
path: output/{{ item.0 }}/{{ item.0 }}_{{ item.1.type }}.txt
state: absent
regexp: "{{ item.2 }}"
loop: "{{ group_names | product(dns_blocklists_lists, _dns_blocklists_exclude) | list }}"
loop_control:
label: "{{ item.2 }}"
vars:
_dns_blocklists_exclude:
- mullvad.net
- angband.live # Reported via Mastodon as incorrect
- name: Build zonefiles
hosts: all
become: false
tags: zonefile
connection: local
gather_facts: true
run_once: true
tasks:
- 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.mullvadvpn.net. (
{{ now(fmt='%s') }} ; serial
86400 ; refresh
7200 ; retry
2592000 ; 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 }}"