diff --git a/roles/network-loopback/tasks/main.yml b/roles/network-loopback/tasks/main.yml index 0dd7564..47e931f 100644 --- a/roles/network-loopback/tasks/main.yml +++ b/roles/network-loopback/tasks/main.yml @@ -4,3 +4,17 @@ src: loopback.j2 dest: "/etc/network/interfaces.d/loopback" notify: reload network interfaces + +- name: create check-anycast.sh + template: + src: check-anycast.sh.j2 + dest: "/usr/local/bin/check-anycast.sh" + owner: root + group: root + mode: 0755 + +- name: create cronjob for check-anycast.sh" + cron: + name: "check anycast availability" + minute: "*/1" + job: "/usr/local/bin/check-anycast.sh > /dev/null 2>&1" diff --git a/roles/network-loopback/templates/check-anycast.sh.j2 b/roles/network-loopback/templates/check-anycast.sh.j2 new file mode 100644 index 0000000..87f8415 --- /dev/null +++ b/roles/network-loopback/templates/check-anycast.sh.j2 @@ -0,0 +1,22 @@ +#!/bin/bash + +LOOPBACK="loopback" +ANYCAST="{{ anycast_ipv4 | ipaddr('address') }}" + +ANYCAST_PRESENT=false +if ping -c 1 "${ANYCAST}" > /dev/null 2>&1 ; then + ANYCAST_PRESENT=true +fi + +DNS_PRESENT=false +if /sbin/resolvconf -l "${LOOPBACK}" > /dev/null 2>&1 ; then + DNS_PRESENT=true +fi + +if [[ ${ANYCAST_PRESENT} == true && ${DNS_PRESENT} == true ]] ; then + exit 0 +elif [[ ${ANYCAST_PRESENT} == true && ${DNS_PRESENT} == false ]] ; then + echo "nameserver ${ANYCAST}" | /sbin/resolvconf -a "${LOOPBACK}" -m 1 +elif [[ ${ANYCAST_PRESENT} == false && ${DNS_PRESENT} == true ]] ; then + /sbin/resolvconf -d "${LOOPBACK}" +fi