From f985755b32cc3f3558604264823de32de557b288 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 24 Nov 2018 20:31:54 +0100 Subject: o Adding Linode update code. --- ansible/roles/linode-dns-update/handlers/main.yml | 10 ++++ ansible/roles/linode-dns-update/tasks/main.yml | 65 ++++++++++++++++++++++ .../templates/linode-dns-update.j2 | 45 +++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 ansible/roles/linode-dns-update/handlers/main.yml create mode 100644 ansible/roles/linode-dns-update/tasks/main.yml create mode 100644 ansible/roles/linode-dns-update/templates/linode-dns-update.j2 (limited to 'ansible/roles/linode-dns-update') diff --git a/ansible/roles/linode-dns-update/handlers/main.yml b/ansible/roles/linode-dns-update/handlers/main.yml new file mode 100644 index 0000000..9d7ba98 --- /dev/null +++ b/ansible/roles/linode-dns-update/handlers/main.yml @@ -0,0 +1,10 @@ +- name: systemd daemon-reload + become: yes + systemd: + daemon_reload: true + +- name: service start linode-dns-update.timer + become: yes + service: + name: linode-dns-update.timer + state: restarted diff --git a/ansible/roles/linode-dns-update/tasks/main.yml b/ansible/roles/linode-dns-update/tasks/main.yml new file mode 100644 index 0000000..8b305f8 --- /dev/null +++ b/ansible/roles/linode-dns-update/tasks/main.yml @@ -0,0 +1,65 @@ +- when: (linode_dns__ipv4_resource is defined) or (linode_dns__ipv6_resource is defined) + become: true + tags: linode-dns-update + block: + - name: /usr/local/bin/linode-dns-update + template: + src: linode-dns-update.j2 + dest: /usr/local/bin/linode-dns-update + owner: root + group: root + mode: a=rx,u=rwx + + - name: /etc/systemd/system/linode-dns-update.service + notify: + - systemd daemon-reload + copy: + dest: /etc/systemd/system/linode-dns-update.service + content: | + [Unit] + Description=Update DNS entry + + [Service] + Type=oneshot + ExecStart=/usr/local/bin/linode-dns-update + User=nobody + Group=systemd-journal + + - name: systemctl enable linode-dns-update.service + systemd: + name: linode-dns-update.service + enabled: yes + + - name: /etc/systemd/system/linode-dns-update.timer + notify: + - systemd daemon-reload + - service start linode-dns-update.timer + copy: + dest: /etc/systemd/system/linode-dns-update.timer + content: | + [Unit] + Description=Update DNS entry + + [Timer] + OnBootSec=5min + OnUnitActiveSec=1hour + + [Install] + WantedBy=timers.target + + - name: systemctl enable linode-dns-update.timer + systemd: + name: linode-dns-update.timer + enabled: yes + state: started + + # Remove old stuff + - file: + path: /usr/local/bin/update-linode + state: absent + + - name: Install cron job + cron: + name: update-linode + cron_file: update-linode + state: absent diff --git a/ansible/roles/linode-dns-update/templates/linode-dns-update.j2 b/ansible/roles/linode-dns-update/templates/linode-dns-update.j2 new file mode 100644 index 0000000..97ab7c9 --- /dev/null +++ b/ansible/roles/linode-dns-update/templates/linode-dns-update.j2 @@ -0,0 +1,45 @@ +#!/bin/bash + +set -e + +# Hostname: {{ ansible_hostname }} +URL="https://api.linode.com/" +USERNAME="{{ linode_dns__api_username }}" +KEY="{{ linode_dns__api_key }}" +DOMAIN_ID="{{ linode_dns__domain_id }}" + +PATH=/sbin:/usr/sbin:/bin:/usr/bin: + +req() { + PARAMS=() + for p in "$@" + do + PARAMS+=(-F "$p") + done + + curl \ + -4 \ + --user "$USERNAME:$KEY" -s \ + "${PARAMS[@]}" \ + "$URL" >/dev/null # | jq +} + +domain_resource_update() { + req api_action=domain.resource.update DomainId="$1" ResourceId="$2" Type="$3" Target="$4" +} + +{% if linode_dns__ipv4_resource is defined %} +domain_resource_update "$DOMAIN_ID" "{{ linode_dns__ipv4_resource }}" "A" "[remote_addr]" +{% endif %} + +{% if linode_dns__ipv6_resource is defined %} +q='' +#q='map(select(.ifname=="wlx00e01d0808b2"))' +q='map(.addr_info) | flatten | map(select(.scope=="global" and .deprecated != true and .mngtmpaddr != true) | .local) | first' +ip=$(ip -6 -json addr | jq -r -c "$q") + +if [[ ! -z $ip ]] +then + domain_resource_update "$DOMAIN_ID" "{{ linode_dns__ipv6_resource }}" "AAAA" "$ip" +fi +{% endif %} -- cgit v1.2.3