-
Notifications
You must be signed in to change notification settings - Fork 18
/
setup-external-dns.yml
195 lines (171 loc) · 10.9 KB
/
setup-external-dns.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
---
- name: "Setup DNS"
hosts: localhost
connection: local
tasks:
- set_fact:
records:
"netsoc.co":
# A
- { type: A, name: "cix-0", value: "84.39.234.50" }
- { type: A, name: "cix-1", value: "84.39.234.51" }
- { type: A, name: "cix-2", value: "84.39.234.52" }
- { type: A, name: "cix-3", value: "84.39.234.53" }
- { type: A, name: "cix-4", value: "84.39.234.54" }
- { type: A, name: "ucc-0", value: "143.239.87.40" }
- { type: A, name: "ucc-1", value: "143.239.87.41" }
# Historical hostnames
- { type: A, name: "bigbertha", value: "84.39.234.50" }
- { type: A, name: "boole", value: "84.39.234.54" }
- { type: A, name: "feynman", value: "84.39.234.53" }
- { type: A, name: "leela", value: "84.39.234.51" }
- { type: A, name: "lovelace", value: "84.39.234.52" }
- { type: A, name: "elon", value: "143.239.87.40" }
- { type: A, name: "tesla", value: "143.239.87.41" }
# handbook
- { type: CNAME, name: handbook, value: "uccnetsoc.github.io" }
# status
- { type: CNAME, name: status, value: "uccnetsoc.github.io" }
# For old ldap
- { type: CNAME, name: ldap, value: elon.netsoc.co }
# To cix-3.netsoc.co
- { type: CNAME, name: control, value: "cix-3.netsoc.co" }
- { type: CNAME, name: minecraft, value: "cix-3.netsoc.co" }
# To cix-1.netsoc.co
- { type: CNAME, name: leela-vm, value: "cix-1.netsoc.co" }
- { type: CNAME, name: admin, value: "leela-vm.netsoc.co" }
- { type: CNAME, name: mysql, value: "leela-vm.netsoc.co" }
- { type: CNAME, name: "*.evan", value: "leela-vm.netsoc.co" }
- { type: CNAME, name: www.*.evan, value: "leela-vm.netsoc.co" }
# To cix-1.netsoc.co
- { type: CNAME, name: web, value: "cix-1.netsoc.co" }
- { type: CNAME, name: netsoc.co, value: "web.netsoc.co" }
- { type: CNAME, name: ipa, value: "web.netsoc.co" }
- { type: CNAME, name: keycloak, value: "web.netsoc.co" }
- { type: CNAME, name: semaphore, value: "web.netsoc.co" }
- { type: CNAME, name: traefik, value: "web.netsoc.co" }
- { type: CNAME, name: prometheus, value: "web.netsoc.co" }
- { type: CNAME, name: ci, value: "web.netsoc.co" }
- { type: CNAME, name: grafana, value: "web.netsoc.co" }
- { type: CNAME, name: loki, value: "web.netsoc.co" }
- { type: CNAME, name: bot, value: "web.netsoc.co" }
- { type: CNAME, name: wiki, value: "web.netsoc.co" }
- { type: CNAME, name: whodis, value: "web.netsoc.co" }
- { type: CNAME, name: links, value: "web.netsoc.co" }
- { type: CNAME, name: www, value: "web.netsoc.co" }
- { type: CNAME, name: mentorships, value: "web.netsoc.co" }
- { type: CNAME, name: tutorial, value: "web.netsoc.co" }
- { type: CNAME, name: constitution, value: "web.netsoc.co" }
- { type: CNAME, name: portal, value: "web.netsoc.co" }
- { type: CNAME, name: blog, value: "web.netsoc.co" }
- { type: CNAME, name: discord, value: "web.netsoc.co" }
- { type: CNAME, name: esports, value: "web.netsoc.co" }
- { type: CNAME, name: cloud, value: "web.netsoc.co" }
- { type: CNAME, name: "*", value: "web.netsoc.co" }
- { type: CNAME, name: www.*, value: "web.netsoc.co" }
- { type: CNAME, name: build, value: "web.netsoc.co" }
# MX
- { type: MX, name: "netsoc.co", value: "mx.zoho.com" }
- { type: MX, name: "netsoc.co", value: "mx2.zoho.com" }
- { type: MX, name: "blog.netsoc.co", value: "mxa.eu.mailgun.org" }
- { type: MX, name: "blog.netsoc.co", value: "mxb.eu.mailgun.org" }
# TXT
- { type: TXT, name: "_dmarc", value: "v=DMARC1; p=none"}
- { type: TXT, name: "_github-challenge-uccnetsoc", value: "58926b4c4e"}
- { type: TXT, name: "netsoc.co", value: "v=spf1 mx include:zoho.com include:u18778499.wl250.sendgrid.net -all"}
- { type: TXT, name: "zoho._domainkey", value: "k=rsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCB3sWPAMJHYvSCWEueINNaIsPy4OeAQgFsm3y+IcOSUcXJUhHr+LrYGuD9SZzIEh0ekm0Usm73gRz4LgGYfeZ9SXbR3H1LupOETGym0FRRQnAjnXI9L/wfaOzYwbpKm04lhpbobhAwrtsUcgMyo+wmEByM3AR25+YDmlUuijmNTwIDAQAB" }
- { type: TXT, name: "blog.netsoc.co", value: "v=spf1 include:mailgun.org ~all"}
- { type: TXT, name: "email._domainkey.blog.netsoc.co", value: "k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDc8qQcyiS0SM47o6W3yzGAT9dT3f5uyEGnYvljOU05nq7tcpBcxatOrJS0ADhqCBr/o20K4s1BKYecR9ChXOWQkDSpw67piVWlg9ikoOMv9B3jtu1w/z5FrGYX1m5lVYPkY6p7M9FRYF+CM26oqjl0Al11nhfu/h9EWDQCGzXaQQIDAQAB" }
# Email
- { type: CNAME, name: "em9504", value: "u18778499.wl250.sendgrid.net"}
- { type: CNAME, name: "s1._domainkey", value: "s1.domainkey.u18778499.wl250.sendgrid.net" }
- { type: CNAME, name: "s2._domainkey", value: "s2.domainkey.u18778499.wl250.sendgrid.net" }
- { type: CNAME, name: "email.blog.netsoc.co", value: "eu.mailgun.org" }
# SRV
- { type: SRV, name: "minecraft", proto: "tcp", service: "minecraft", port: "1194", priority: 0, weight: 5, value: "minecraft.netsoc.co"}
- { type: SRV, name: "build", proto: "tcp", service: "minecraft", port: "16938", priority: 0, weight: 5, value: "build.netsoc.co"}
"netsoc.cloud":
- { type: CNAME, name: "netsoc.cloud", value: "web.netsoc.co" }
- { type: CNAME, name: api, value: "web.netsoc.co" }
- { type: CNAME, name: proxy, value: "web.netsoc.co" }
- { type: CNAME, name: ssh, value: "web.netsoc.co" }
- { type: CNAME, name: sftp, value: "web.netsoc.co" }
- { type: CNAME, name: "*", value: "web.netsoc.co" }
"netsoc.dev":
# A
- { type: CNAME, name: "*", value: "web.netsoc.co" }
cf_headers:
X-Auth-Email: "{{ cloudflare.api.email }}"
Authorization: "Bearer {{ cloudflare.api.dns_api_token }}"
Content-Type: "application/json"
grafana_basic_auth: "{{ grafana.username }}:{{ grafana.password }}"
# - name: "Set Grafana dashboard annotation"
# uri:
# url: https://grafana.netsoc.co/api/annotations
# method: POST
# headers:
# Content-Type: "application/json"
# Accept: "application/json"
# Authorization: "Basic {{ grafana_basic_auth | b64encode }}"
# body_format: json
# body: '{
# "panelId": 10,
# "dashboardId": 25,
# "time": {{ ansible_date_time.epoch | int * 1000 }},
# "tags": [ "ansible" ],
# "text": "Ansible setup-external-dns.yml playbook started",
# }'
- name: Get zone details for all domains in `records`
uri:
url: "https://api.cloudflare.com/client/v4/zones?name={{ records | join(',') }}"
return_content: yes
headers: "{{ cf_headers }}"
register: zone_details
- name: Get all dns records for domains in `records` using zone id
uri:
url: "https://api.cloudflare.com/client/v4/zones/{{ item }}/dns_records?per_page=100"
return_content: yes
headers: "{{ cf_headers }}"
loop: "{{ zone_details.json.result | map(attribute='id') | list }}"
register: zone_records_result
# Flatten records for all zones and for every record check if name,type,value match any record in playbook in that zone
- name: Delete records not in playbook
uri:
url: "https://api.cloudflare.com/client/v4/zones/{{ item.zone_id }}/dns_records/{{ item.id }}"
method: "DELETE"
headers: "{{ cf_headers }}"
when: >
{'name': item.name | regex_replace('(.+?)\\.?' + item.zone_name + '$', '\\1'), 'type': item.type, 'value': item.content} not in records[item.zone_name]
with_items: "{{ zone_records_result.results | map(attribute='json.result') | flatten }}"
loop_control:
label: "{{ item.zone_name }} {{ item.type }} {{ item.name }} {{ item.content }}"
- name: Refresh dns records
uri:
url: "https://api.cloudflare.com/client/v4/zones/{{ item }}/dns_records?per_page=100"
return_content: yes
headers: "{{ cf_headers }}"
loop: "{{ zone_details.json.result | map(attribute='id') | list }}"
register: zone_records_result
# For every record in playbook create it if cloudflare does not have it
# Use json_query to extract name,content,type,zone from CF API results
# - CF API uses `zone_name` and Ansible cloudflare_dns uses `zone` so change key to `zone`
# - CF API uses `content` but we use `value`. Ansible cf_dns module has content/value aliased
# For comparison append zone to record name (unless name == zone) as CF API uses full name e.g. sub.example.net
- name: Create missing records
cloudflare_dns: "{{ item | combine({'account_email': cloudflare.api.email, 'account_api_token': cloudflare.api.key}) }}"
loop: >
[
{% set cf_records = zone_records_result.results | map(attribute='json.result') | flatten | json_query("[].{name: name, value: content, type: type, zone: zone_name}") %}
{% for zone in records %}
{% for record in records[zone] %}
{% set check = {'value': record.value, 'name': record.name | regex_replace('(?!'+zone+')^(.*)$', '\\1.' + zone), 'zone': zone, 'type': record.type} %}
{% if check not in cf_records %}
{{ record | combine({'zone': zone}) }},
{% endif %}
{% endfor %}
{% endfor %}
]
vars_files:
- vars/network.yml
- vars/secrets_mapping.yml
- vars/secrets.yml