summaryrefslogtreecommitdiff
path: root/roles/ddns.notyet
diff options
context:
space:
mode:
Diffstat (limited to 'roles/ddns.notyet')
-rw-r--r--roles/ddns.notyet/meta/main.yml3
-rw-r--r--roles/ddns.notyet/tasks/main.yml11
-rw-r--r--roles/ddns.notyet/templates/update-dns.j224
-rw-r--r--roles/ddns.notyet/vars/main.yml2
-rw-r--r--roles/ddns.notyet/vars/vault.yml8
5 files changed, 48 insertions, 0 deletions
diff --git a/roles/ddns.notyet/meta/main.yml b/roles/ddns.notyet/meta/main.yml
new file mode 100644
index 0000000..fdda41b
--- /dev/null
+++ b/roles/ddns.notyet/meta/main.yml
@@ -0,0 +1,3 @@
+---
+dependencies:
+ - role: common
diff --git a/roles/ddns.notyet/tasks/main.yml b/roles/ddns.notyet/tasks/main.yml
new file mode 100644
index 0000000..63caa0c
--- /dev/null
+++ b/roles/ddns.notyet/tasks/main.yml
@@ -0,0 +1,11 @@
+---
+- include_vars: vars/vault.yml
+
+- name: install dns utilities
+ apt: name=dnsutils state=latest
+
+- name: install jq
+ apt: name=jq state=latest
+
+- name: schedule periodic updates of dns entry
+ template: src=update-dns.j2 dest=/etc/cron.daily/update-dns
diff --git a/roles/ddns.notyet/templates/update-dns.j2 b/roles/ddns.notyet/templates/update-dns.j2
new file mode 100644
index 0000000..4d68666
--- /dev/null
+++ b/roles/ddns.notyet/templates/update-dns.j2
@@ -0,0 +1,24 @@
+#!/bin/sh
+set -e
+
+api_email=jodersky@gmail.com
+api_key={{ddns_api_key}}
+zone_name={{ddns_zone}}
+record_name={{ddns_record}}
+
+cf() {
+ curl \
+ -sS \
+ -H "X-Auth-Email: $api_email"\
+ -H "X-Auth-Key: $api_key"\
+ -H "Content-Type: application/json"\
+ $@
+}
+
+external_ip=$(dig +short myip.opendns.com @resolver1.opendns.com)
+zone_id=$(cf -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" | jq -r '.result[0].id')
+record_id=$(cf -X GET "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?name=$record_name" | jq -r '.result[0].id')
+
+cf -X PUT "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records/${record_id}" \
+ --data {\"type\":\"A\",\"name\":\""$record_name"\",\"content\":\""$external_ip"\"} \
+ || (echo "Error updating IP address." >&2 && exit 1)
diff --git a/roles/ddns.notyet/vars/main.yml b/roles/ddns.notyet/vars/main.yml
new file mode 100644
index 0000000..6128462
--- /dev/null
+++ b/roles/ddns.notyet/vars/main.yml
@@ -0,0 +1,2 @@
+---
+ddns_api_key: "{{vault_ddns_api_key}}"
diff --git a/roles/ddns.notyet/vars/vault.yml b/roles/ddns.notyet/vars/vault.yml
new file mode 100644
index 0000000..77502f8
--- /dev/null
+++ b/roles/ddns.notyet/vars/vault.yml
@@ -0,0 +1,8 @@
+$ANSIBLE_VAULT;1.1;AES256
+38333565623838383936376530366331383234626234346666623232643831333761376563666561
+3930343438613136656633656363633930623931626238330a656236633037303535663733383838
+33636566336164313365653766353931353739343562343435613130623739656432383831323466
+3039626461333738610a303632633562326133356635656234353334343764636236623238343262
+39623638376663643964623938626238626636313136636364633561346630303266303232363366
+33383361623532636165666433653964653937613038393132343762666131616338643230643734
+313734343834663538323038393337316635