From 10a39235bb91649e143c6e90f63b74e30ea6a6b6 Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Wed, 26 Aug 2020 20:26:05 -0400 Subject: [PATCH] Update DuckDNS subdomain with dynamic IP --- roles/base/tasks/duckdns.yml | 11 +++++++++++ roles/base/tasks/main.yml | 4 ++++ roles/base/templates/duckdns.sh.j2 | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 roles/base/tasks/duckdns.yml create mode 100644 roles/base/templates/duckdns.sh.j2 diff --git a/roles/base/tasks/duckdns.yml b/roles/base/tasks/duckdns.yml new file mode 100644 index 0000000..09ac975 --- /dev/null +++ b/roles/base/tasks/duckdns.yml @@ -0,0 +1,11 @@ +- name: Install DuckDNS script + template: + src: duckdns.sh.j2 + dest: /usr/local/bin/update-duckdns + mode: 0700 + +- name: Install DuckDNS cron + cron: + name: Update DuckDNS + minute: "*/5" + job: /usr/local/bin/update-duckdns diff --git a/roles/base/tasks/main.yml b/roles/base/tasks/main.yml index fe95f15..f9d5329 100644 --- a/roles/base/tasks/main.yml +++ b/roles/base/tasks/main.yml @@ -7,3 +7,7 @@ - import_tasks: network.yml tags: network when: manage_network + +- import_tasks: duckdns.yml + tags: duckdns + when: duckdns is defined diff --git a/roles/base/templates/duckdns.sh.j2 b/roles/base/templates/duckdns.sh.j2 new file mode 100644 index 0000000..94ce9d4 --- /dev/null +++ b/roles/base/templates/duckdns.sh.j2 @@ -0,0 +1,20 @@ +#!/bin/bash + +domains="{{ duckdns.domains }}" +token="{{ duckdns.token }}" +{% if duckdns.logfile is undefined -%} + log="/var/log/duckdns.log" +{% else -%} + log="{{ duckdns.logfile }}" +{% endif %} + +url="https://www.duckdns.org/update?domains=$domains&token=$token&ip=" + +time=$(date '+%Y%m%d-%H%M%S') +result=$(curl $url) + +if [ "$result" = "OK" ]; then + echo "[$time] OK" >> $log +else + echo "[$time] ERROR: $result" >> $log +fi