refactor ♻️: Refactor task logic for detecting and validating physical NICs enslaved to a bridge

This commit refactors the task logic to include detection and validation of physical NICs enslaved to a bridge, selects the final WoL interface, and updates debug and failure messages accordingly. It also adds variables for detected bridge and PHY in `defaults/main.yml`.
This commit is contained in:
2025-12-21 18:23:12 +01:00
parent 000c0749a3
commit ae7ed6a894
2 changed files with 39 additions and 29 deletions

View File

@@ -1,5 +1,8 @@
# defaults/main.yml
# Network interface to enable Wake-on-LAN on # Network interface to enable Wake-on-LAN on
wol_interface: "" wol_interface: "" # user override (physical NIC)
wol_detected_bridge: ""
wol_detected_phy: ""
# WOL mode: # WOL mode:
# g = magic packet (most common) # g = magic packet (most common)

View File

@@ -13,44 +13,51 @@
- name: Detect default route interface - name: Detect default route interface
ansible.builtin.set_fact: ansible.builtin.set_fact:
wol_detected_interface: "{{ ansible_default_ipv4.interface }}" wol_detected_interface: "{{ ansible_default_ipv4.interface }}"
when: wol_interface | length == 0 when: wol_interface | default('') | length == 0
- name: Debug default route interface - name: Detect physical NIC enslaved to bridge
ansible.builtin.debug:
msg: "Detected default route: {{ wol_detected_interface }}"
- name: Validate detected interface is physical
ansible.builtin.set_fact: ansible.builtin.set_fact:
wol_interface_final: "{{ wol_detected_interface }}" wol_detected_phy: "{{ item }}"
loop: "{{ ansible_interfaces }}"
when: when:
- wol_interface | length == 0 - wol_interface | default('') | length == 0
- wol_detected_interface is defined - item != 'lo'
- wol_detected_interface is not match("^vmbr") - hostvars[inventory_hostname]['ansible_' + item] is defined
- wol_detected_interface is not match("^lo") - hostvars[inventory_hostname]['ansible_' + item].master is defined
- wol_detected_interface is not match("^bond") - hostvars[inventory_hostname]['ansible_' + item].master == wol_detected_bridge
- wol_detected_interface is not match("^tap")
- wol_detected_interface is not match("^veth")
- name: Use manually defined interface if provided - name: Select final WoL interface
ansible.builtin.set_fact: ansible.builtin.set_fact:
wol_interface_final: "{{ wol_interface }}" wol_final_interface: >-
when: wol_interface | length > 0 {{
wol_interface
if wol_interface | default('') | length > 0
else wol_detected_phy
}}
- name: Fail if no suitable NIC was found - name: Fail if no physical NIC was detected
ansible.builtin.fail: ansible.builtin.fail:
msg: > msg: >
Unable to determine a suitable physical NIC for Wake-on-LAN. Unable to detect a physical NIC for Wake-on-LAN.
Please set wol_interface manually. Detected bridge: {{ wol_detected_bridge | default('none') }}.
when: wol_interface_final is not defined Please set wol_interface explicitly.
when: wol_final_interface | default('') | length == 0
- name: Debug WoL interface selection
ansible.builtin.debug:
msg:
- "Detected bridge: {{ wol_detected_bridge }}"
- "Detected physical NIC: {{ wol_detected_phy }}"
- "Final WoL interface: {{ wol_final_interface }}"
- name: Check WOL support on interface - name: Check WOL support on interface
ansible.builtin.command: ethtool {{ wol_interface_final }} ansible.builtin.command: ethtool {{ wol_final_interface }}
register: wol_capabilities register: wol_capabilities
changed_when: false changed_when: false
- name: Fail if NIC does not support Wake-on-LAN - name: Fail if NIC does not support Wake-on-LAN
ansible.builtin.fail: ansible.builtin.fail:
msg: "Interface {{ wol_interface_final }} does not support Wake-on-LAN." msg: "Interface {{ wol_final_interface }} does not support Wake-on-LAN."
when: "'Supports Wake-on-Wake-on-LAN' in wol_capabilities.stdout or when: "'Supports Wake-on-Wake-on-LAN' in wol_capabilities.stdout or
'Wake-on:' not in wol_capabilities.stdout" 'Wake-on:' not in wol_capabilities.stdout"
@@ -66,11 +73,11 @@
- Enable and start WOL service - Enable and start WOL service
- name: Enable WOL immediately (without reboot) - name: Enable WOL immediately (without reboot)
ansible.builtin.command: ethtool -s {{ wol_interface_final }} wol {{ wol_mode }} ansible.builtin.command: ethtool -s {{ wol_final_interface }} wol {{ wol_mode }}
changed_when: false changed_when: false
- name: Verify Wake-on-LAN status - name: Verify Wake-on-LAN status
ansible.builtin.command: ethtool {{ wol_interface_final }} ansible.builtin.command: ethtool {{ wol_final_interface }}
register: wol_status register: wol_status
changed_when: false changed_when: false
when: wol_verify when: wol_verify
@@ -79,7 +86,7 @@
set_fact: set_fact:
wol_mac_address: >- wol_mac_address: >-
{{ {{
ansible_facts['ansible_' + wol_interface_final]['macaddress'] ansible_facts['ansible_' + wol_final_interface]['macaddress']
| default('') | default('')
}} }}
@@ -90,7 +97,7 @@
- name: Fail if MAC address could not be detected - name: Fail if MAC address could not be detected
fail: fail:
msg: "Unable to determine MAC address for interface {{ wol_interface_final }}" msg: "Unable to determine MAC address for interface {{ wol_final_interface }}"
when: when:
- wol_report_mac - wol_report_mac
- wol_mac_address | length == 0 - wol_mac_address | length == 0
@@ -98,7 +105,7 @@
- name: Report Wake-on-LAN sender details - name: Report Wake-on-LAN sender details
ansible.builtin.debug: ansible.builtin.debug:
msg: msg:
- "Wake-on-LAN enabled on interface: {{ wol_interface_final }}" - "Wake-on-LAN enabled on interface: {{ wol_final_interface }}"
- "MAC address: {{ wol_mac_address }}" - "MAC address: {{ wol_mac_address }}"
- "Example WOL commands:" - "Example WOL commands:"
- " wakeonlan {{ wol_mac_address }}" - " wakeonlan {{ wol_mac_address }}"