/
/
/
Ansible role that sets up sane defaults based on a simple host configuration.
1# {{ ansible_managed }}
2[Unit]
3Description=Check WiFi connectivity and reconnect if the LAN is unreachable
4After=network-online.target
5Wants=network-online.target
6
7[Service]
8Type=oneshot
9
10# **Test the LAN, not the internet.**
11#
12# This unit originally pinged {{ system_wifi_wan_target }} alone, which is the
13# wrong question twice over. The devices this runs on talk to a broker, a
14# registry and the runner -- all on 192.168.34.0/24. They do not need the
15# internet at all.
16#
17# - A WAN outage with a healthy LAN bounced a working link, taking the
18# table down for a reason that had nothing to do with it.
19# - A LAN-side failure while the WAN still answered went undetected, which
20# is the case this unit exists for.
21#
22# So the LAN check alone decides whether to reconnect. The WAN is probed only
23# to label the log line, because "LAN down, WAN up" and "both down" point at
24# different faults and the distinction is free to record.
25ExecStart=/bin/bash -c '\
26 lan_target="{{ system_wifi_lan_target | default("", true) }}"; \
27 if [ -z "$lan_target" ]; then \
28 lan_target=$(/sbin/ip route show default 0.0.0.0/0 dev {{ system_wifi_interface }} \
29 | /usr/bin/awk "/default/ {print \\$3; exit}"); \
30 fi; \
31 if [ -z "$lan_target" ]; then \
32 /usr/bin/logger -t wifi-reconnect "no default gateway on {{ system_wifi_interface }}; reconnecting"; \
33 /usr/bin/nmcli device disconnect {{ system_wifi_interface }}; \
34 /bin/sleep 2; \
35 /usr/bin/nmcli device connect {{ system_wifi_interface }}; \
36 exit 0; \
37 fi; \
38 if /bin/ping -c{{ system_wifi_check_count }} -W{{ system_wifi_check_timeout }} "$lan_target" >/dev/null 2>&1; then \
39 exit 0; \
40 fi; \
41 if /bin/ping -c{{ system_wifi_check_count }} -W{{ system_wifi_check_timeout }} {{ system_wifi_wan_target }} >/dev/null 2>&1; then \
42 wan="up"; \
43 else \
44 wan="down"; \
45 fi; \
46 /usr/bin/logger -t wifi-reconnect "LAN gateway $lan_target unreachable (WAN {{ system_wifi_wan_target }} $wan); reconnecting {{ system_wifi_interface }} via NetworkManager"; \
47 /usr/bin/nmcli device disconnect {{ system_wifi_interface }}; \
48 /bin/sleep 2; \
49 /usr/bin/nmcli device connect {{ system_wifi_interface }}; \
50 /usr/bin/logger -t wifi-reconnect "reconnect issued for {{ system_wifi_interface }}"'
51