diff options
| author | Joe Carstairs <me@joeac.net> | 2026-08-28 15:23:36 +0100 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2026-08-28 15:23:36 +0100 |
| commit | 4931117343a3105749b4a3698dd24c824a505441 (patch) | |
| tree | fd4c10ac16fc37e694bea8fced8eb5aea55cc966 /roles/domain-zone-management | |
| parent | 2a15e073bceaaf0765343dd57931c85cada6f89c (diff) | |
ovh-ify dyndns
Diffstat (limited to 'roles/domain-zone-management')
| -rw-r--r-- | roles/domain-zone-management/tasks/main.yml | 34 | ||||
| -rw-r--r-- | roles/domain-zone-management/templates/ovh.conf | 7 | ||||
| -rwxr-xr-x | roles/domain-zone-management/templates/update_ovhcloud_domain_zone | 156 |
3 files changed, 197 insertions, 0 deletions
diff --git a/roles/domain-zone-management/tasks/main.yml b/roles/domain-zone-management/tasks/main.yml new file mode 100644 index 0000000..c3592e4 --- /dev/null +++ b/roles/domain-zone-management/tasks/main.yml @@ -0,0 +1,34 @@ +- name: Install jq + community.general.apk: + name: jq + +- name: Install OVHCloud CLI + ansible.builtin.shell: + cmd: curl -fsSL https://raw.githubusercontent.com/ovh/ovhcloud-cli/main/install.sh | sh + creates: /usr/bin/ovhcloud + +- name: Install OVHCloud CLI config + ansible.builtin.template: + src: ovh.conf + dest: /etc/ovh.conf + +- name: Create OVH domain zone append records directory + ansible.builtin.file: + path: "{{ ovh_domain_zone_append_records_directory }}" + state: directory + +- name: Create OVH domain zone replace records directory + ansible.builtin.file: + path: "{{ ovh_domain_zone_replace_records_directory }}" + state: directory + +- name: Create OVH domain zone log directory + ansible.builtin.file: + path: "{{ ovh_domain_zone_log_directory }}" + state: directory + +- name: Install OVHCloud domain zone management script + ansible.builtin.template: + src: update_ovhcloud_domain_zone + dest: /usr/bin/update_ovhcloud_domain_zone + mode: "755" diff --git a/roles/domain-zone-management/templates/ovh.conf b/roles/domain-zone-management/templates/ovh.conf new file mode 100644 index 0000000..a2c3739 --- /dev/null +++ b/roles/domain-zone-management/templates/ovh.conf @@ -0,0 +1,7 @@ +[default] +endpoint={{ ovhcloud_endpoint }} + +[ovh-eu] +application_key={{ ovhcloud_application_key }} +application_secret={{ ovhcloud_application_secret }} +consumer_key={{ ovhcloud_consumer_key }} diff --git a/roles/domain-zone-management/templates/update_ovhcloud_domain_zone b/roles/domain-zone-management/templates/update_ovhcloud_domain_zone new file mode 100755 index 0000000..dfeac61 --- /dev/null +++ b/roles/domain-zone-management/templates/update_ovhcloud_domain_zone @@ -0,0 +1,156 @@ +#!/bin/sh + + +SCRIPT="$0" +SCRIPT_DIR=$(cd "${SCRIPT%/*}" && pwd ) +SCRIPT_NAME=${SCRIPT##*/} +APPEND_RECORDS_DIR="{{ ovh_domain_zone_append_records_directory }}" +REPLACE_RECORDS_DIR="{{ ovh_domain_zone_replace_records_directory }}" +mkdir -p ${APPEND_RECORDS_DIR} +mkdir -p ${REPLACE_RECORDS_DIR} + +cut_nth_word() +{ + echo "$1" | cut -d ' ' -f "$2" +} + +get_record_subdomain() +{ + record="$1" + subdomain="$(cut_nth_word "$record" 1)" + if test "$(echo "$subdomain" | grep -q [A-Z])" + then + subdomain="$(cut_nth_word "$record" 2)" + fi + if test "$subdomain" == "@" + then + subdomain="" + fi + echo "$subdomain" +} + +get_record_type() +{ + record="$1" + record_type="$(cut_nth_word "$record" 2)" + if test "$(echo "$record_type" | grep -q [0-9])" + then + record_type="$(cut_nth_word "$record" 1)" + fi + echo "$record_type" +} + +get_record_target() +{ + record="$1" + target_and_ttl="$(cut_nth_word "$record" "3-")" + target="$(cut_nth_word "$(echo "$target_and_ttl" | rev)" "2-" | rev)" + echo "$target" +} + +get_record_ttl() +{ + record="$1" + ttl="$(cut_nth_word "$(echo "$record" | rev)" 1 | rev)" + echo "$ttl" +} + +find_existing_record_ids() +{ + subdomain="$1" + record_type="$2" + target="$3" + query=".subDomain == \"${subdomain}\" and .fieldType == \"${record_type}\"" + if [ -n "$target" ] + then + query="${query} and .target == ${target}" + fi + ovhcloud domain-zone record list joeac.net -o json \ + | jq "map(select($query))" \ + | jq "map(.id)" \ + | grep -Eo [0-9]\+ +} + +append_record() +{ + subdomain="$1" + record_type="$2" + target="$3" + ttl="$4" + echo "Appending record to joeac.net zone: ${record_type} ${subdomain:-@} ${target} ${ttl}" + ovhcloud domain-zone record create joeac.net \ + --field-type "$record_type" \ + --sub-domain "$subdomain" \ + --target "$target" \ + --ttl "$ttl" +} + +replace_records() +{ + subdomain="$1" + target="$2" + ttl="$3" + first_existing_record_id="$4" + echo "Replacing record ${first_existing_record_id} in joeac.net zone: ${record_type} ${subdomain:-@} ${target} ${ttl}" + ovhcloud domain-zone record update joeac.net "$first_existing_record_id" \ + --sub-domain "${subdomain}" \ + --target "${target}" \ + --ttl "${ttl}" + shift 4 + excess_record_ids="$@" + if [ -n "$excess_record_ids" ] + then + for id in "$excess_record_ids" + do + echo "Removing record ${id} in joeac.net zone: ${record_type} ${subdomain:-@}" + ovhcloud domain-zone record delete joeac.net "$id" + done + fi +} + +if test $(ls ${APPEND_RECORDS_DIR} | wc -l) -eq 0 +then + echo "Nothing to append: no zone files in ${APPEND_RECORDS_DIR}" +else + for zone_file in ${APPEND_RECORDS_DIR}/* + do + while read record + do + subdomain="$(get_record_subdomain "$record")" + record_type="$(get_record_type "$record")" + target="$(get_record_target "$record")" + ttl="$(get_record_ttl "$record")" + if test -z "$(find_existing_record_ids "$subdomain" "$record_type" "$target")" + then + append_record "$subdomain" "$record_type" "$target" "$ttl" + fi + done < "${zone_file}" + done +fi + +if test $(ls ${REPLACE_RECORDS_DIR} | wc -l) -eq 0 +then + echo "Nothing to replace: no zone files in ${REPLACE_RECORDS_DIR}" + exit 0 +else + for zone_file in ${REPLACE_RECORDS_DIR}/* + do + while read record + do + subdomain="$(get_record_subdomain "$record")" + record_type="$(get_record_type "$record")" + target="$(get_record_target "$record")" + ttl="$(get_record_ttl "$record")" + + existing_record_ids="$(find_existing_record_ids "$subdomain" "$record_type")" + if test -n "$existing_record_ids" + then + replace_records "$subdomain" "$target" "$ttl" "$existing_record_ids" + else + append_record "$subdomain" "$record_type" "$target" "$ttl" "$existing_record_ids" + fi + done < "${zone_file}" + done +fi + +ovhcloud domain-zone refresh joeac.net |
