From ea622e65c175bb08a816fd93d53a1be2b73b0377 Mon Sep 17 00:00:00 2001 From: Pig Monkey Date: Wed, 15 Jan 2025 18:01:39 -0800 Subject: [PATCH] toggle wifi on ethernet via networkmanager Required only for the brave new post-TLP world. --- group_vars/all | 1 + .../files/wifi-wired-exclusive.sh | 40 +++++++++++++++++++ roles/networkmanager/tasks/main.yml | 7 ++++ 3 files changed, 48 insertions(+) create mode 100755 roles/networkmanager/files/wifi-wired-exclusive.sh diff --git a/group_vars/all b/group_vars/all index cfe7d5e..9e40462 100644 --- a/group_vars/all +++ b/group_vars/all @@ -53,6 +53,7 @@ mail: network: conn_check: False + wifi_wired_exclusive: True trusted_uuid: - 39744b35-2f2f-4a18-9da4-7542a56c28c2 - d2642f7e-9e58-4b00-82f8-26b4ef76a54e diff --git a/roles/networkmanager/files/wifi-wired-exclusive.sh b/roles/networkmanager/files/wifi-wired-exclusive.sh new file mode 100755 index 0000000..9d4cc8c --- /dev/null +++ b/roles/networkmanager/files/wifi-wired-exclusive.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# Stolen from Ilija Matoski: +# https://www.matoski.com/article/wifi-ethernet-autoswitch/ + +name_tag="wifi-wired-exclusive" +syslog_tag="$name_tag" +skip_filename="/etc/NetworkManager/.$name_tag" + +if [ -f "$skip_filename" ]; then + exit 0 +fi + +interface="$1" +action="$2" + +# Bail out if the action is not either up or down. +if [[ "$action" != "up" && "$action" != "down" ]]; then + exit 0 +fi + +iface_type=$(nmcli dev | grep "^$interface" | tr -s ' ' | cut -d' ' -f2) +iface_state=$(nmcli dev | grep "^$interface" | tr -s ' ' | cut -d' ' -f3) + +echo "$syslog_tag: Interface $interface = $iface_state ($iface_type) is $action" + +enable_wifi() { + echo "$syslog_tag: Interface $interface ($iface_type) is down, enabling wifi." + nmcli radio wifi on +} + +disable_wifi() { + echo "$syslog_tag: Disabling wifi, ethernet connection detected." + nmcli radio wifi off +} + +if [[ "$iface_type" = "ethernet" || -z "$iface_type" ]] && [ "$action" = "down" ]; then + enable_wifi +elif [ "$iface_type" = "ethernet" ] && [ "$action" = "up" ] && [ "$iface_state" = "connected" ]; then + disable_wifi +fi diff --git a/roles/networkmanager/tasks/main.yml b/roles/networkmanager/tasks/main.yml index a90f75b..272fa7e 100644 --- a/roles/networkmanager/tasks/main.yml +++ b/roles/networkmanager/tasks/main.yml @@ -19,6 +19,13 @@ src: random_mac.conf.j2 dest: /etc/NetworkManager/conf.d/20-random_mac.conf +- name: Push WiFi/Wired Exclusive dispatcher + copy: + src: wifi-wired-exclusive.sh + dest: /etc/NetworkManager/dispatcher.d/70-wifi-wired-exclusive + mode: 0755 + when: network.wifi_wired_exclusive is defined and network.wifi_wired_exclusive == True + - name: Enable and start NetworkManager service: name: NetworkManager.service