You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hetzner-ansible/roles/_digitalocean/tasks/domain.yml

82 lines
2.2 KiB
YAML

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

---
- name: Read DNS entry for {{ record_name }}.{{ domain }} from digitalocean
uri:
url: "https://api.digitalocean.com/v2/domains/{{ domain }}/records?name={{ record_name }}.{{ domain }}"
headers:
accept: application/json
authorization: Bearer {{ digitalocean_authentication_token }}
return_content: yes
register: domain_records_response
delegate_to: 127.0.0.1
become: false
tags:
- update_dns
- name: Save DNS entry as variable (fact)
set_fact:
domain_records_response_json: "{{ domain_records_response.json }}"
delegate_to: 127.0.0.1
become: false
tags:
- update_dns
- name: Parse DNS entry for {{ record_name }}.{{ domain }}
set_fact:
domain_record: "{{ domain_records_response_json.domain_records | json_query(jmesquery) | first | default({'name': '-', 'ip': '-'}) }}"
vars:
jmesquery: '[*].{id: id, name: name, ip: data}'
delegate_to: 127.0.0.1
become: false
tags:
- update_dns
- name: Print DNS entry for {{ record_name }}.{{ domain }}
debug:
msg: "{{ domain_record }}"
delegate_to: 127.0.0.1
become: false
tags:
- update_dns
- name: Delete DNS entry for <{{ record_data }}:{{ record_name }}> if necessary
uri:
method: DELETE
url: "https://api.digitalocean.com/v2/domains/{{ domain }}/records/{{ domain_record.id }}"
headers:
authorization: Bearer {{ digitalocean_authentication_token }}
return_content: yes
status_code: 204
when:
domain_record.ip != '-'
and record_data != domain_record.ip
delegate_to: 127.0.0.1
become: false
tags:
- update_dns
- name: Create DNS entry for <{{ record_name }}> if necessary
uri:
method: POST
url: "https://api.digitalocean.com/v2/domains/{{ domain }}/records"
headers:
authorization: Bearer {{ digitalocean_authentication_token }}
body: '{{ create_record | to_json }}'
body_format: json
return_content: yes
status_code: 201
vars:
create_record:
type: 'A'
ttl: 1800
data: "{{ record_data }}"
name: "{{ record_name }}"
when:
domain_record.ip == '-'
or record_data != domain_record.ip
or record_name != domain_record.name
delegate_to: 127.0.0.1
become: false
tags:
- update_dns