-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanage_record.yml
More file actions
57 lines (52 loc) · 2.14 KB
/
manage_record.yml
File metadata and controls
57 lines (52 loc) · 2.14 KB
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
---
- name: Manage DYNDNS Record
block:
- name: Lookup if DYNDNS Record exists in Zone {{ dns_zone }}
ansible.builtin.set_fact:
dns_record_id: "{{ item.id }}"
dns_record_type: "{{ item.type }}"
when: item.name == dyndns_name
loop: "{{ dns_records.json.rrsets }}"
- name: Assert that the Record is from Type A
ansible.builtin.assert:
that: dns_record_type == "A"
fail_msg: >
The DNS Record {{ dyndns_name }} already exist with a Record Type of {{ dns_record_type }}.
Needs to be Type A. Please delete the Record manually and run the Playbook again.
success_msg: "DNS Record Type is Type A as expected. Proceeding."
when: dns_record_type | length > 0
- name: DYNDNS Record does NOT exist, will create it with external IP as {{ external_ip_address }}
ansible.builtin.uri:
url: "https://api.hetzner.cloud/v1/zones/{{ zone_id.json.zones[0].id }}/rrsets"
method: POST
headers:
Authorization: "Bearer {{ api_key }}"
body_format: json
body:
name: "{{ dyndns_name }}"
type: "A"
records:
- value: "{{ external_ip_address }}"
comment: ""
ttl: "{{ dyndns_ttl }}"
when: (dns_record_id | length == 0 and external_ip_address | length > 0)
- name: DYNDNS Record does exist, will update it with external IP as {{ external_ip_address }}
ansible.builtin.uri:
url: "https://api.hetzner.cloud/v1/zones/{{ zone_id.json.zones[0].id }}/rrsets/{{ dyndns_name }}/{{ dns_record_type }}/actions/set_records"
method: POST
headers:
Authorization: "Bearer {{ api_key }}"
status_code:
- 200
- 201
body_format: json
body:
records:
- value: "{{ external_ip_address }}"
comment: ""
ttl: "{{ dyndns_ttl }}"
when: (dns_record_id | length > 0 and external_ip_address | length > 0)
rescue:
- name: There was an error
ansible.builtin.debug:
msg: "There was an Error DNS name {{ dyndns_name }} registration. Proceed with next entry!"