2025-12-14 20:46:32 +01:00
|
|
|
---
|
|
|
|
|
- name: Install required packages
|
2025-12-14 21:28:45 +01:00
|
|
|
ansible.builtin.apt:
|
2025-12-14 20:46:32 +01:00
|
|
|
name: ethtool
|
|
|
|
|
state: present
|
|
|
|
|
update_cache: yes
|
|
|
|
|
|
2025-12-23 20:42:03 +01:00
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
# Map vmbr0 → backing physical NIC
|
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
- name: Get bridge link information
|
|
|
|
|
ansible.builtin.command: "bridge link show"
|
|
|
|
|
register: bridge_links
|
|
|
|
|
changed_when: false
|
|
|
|
|
failed_when: false
|
2025-12-21 18:49:25 +01:00
|
|
|
|
2025-12-23 21:24:47 +01:00
|
|
|
- name: Show bridge_links.stdout_lines
|
2025-12-23 21:14:36 +01:00
|
|
|
ansible.builtin.debug:
|
2025-12-23 21:24:47 +01:00
|
|
|
msg: "{{ bridge_links.stdout_lines }}"
|
2025-12-23 21:14:36 +01:00
|
|
|
|
2025-12-23 20:52:54 +01:00
|
|
|
- name: "Detect physical NIC backing {{ wol_bridge }}"
|
2025-12-14 21:28:45 +01:00
|
|
|
ansible.builtin.set_fact:
|
2025-12-23 21:24:47 +01:00
|
|
|
wol_detected_phy_candidates: >-
|
2025-12-21 18:23:12 +01:00
|
|
|
{{
|
2025-12-23 20:42:03 +01:00
|
|
|
bridge_links.stdout_lines
|
|
|
|
|
| select('search', 'master ' ~ wol_bridge)
|
2025-12-23 21:24:47 +01:00
|
|
|
| map('regex_replace', '^\\d+: ([^:@]+).*', '\\1')
|
|
|
|
|
| reject('search', '^(veth|tap|fw)')
|
|
|
|
|
| list
|
2025-12-21 18:23:12 +01:00
|
|
|
}}
|
2025-12-14 21:28:45 +01:00
|
|
|
|
2025-12-23 21:24:47 +01:00
|
|
|
- name: Select first physical NIC candidate
|
|
|
|
|
ansible.builtin.set_fact:
|
|
|
|
|
wol_detected_phy: "{{ wol_detected_phy_candidates[0] | default('') }}"
|
|
|
|
|
|
2025-12-23 20:42:03 +01:00
|
|
|
- name: Fail if vmbr0 backing NIC could not be detected
|
2025-12-14 21:28:45 +01:00
|
|
|
ansible.builtin.fail:
|
|
|
|
|
msg: >
|
2025-12-23 20:42:03 +01:00
|
|
|
Unable to detect physical NIC backing {{ wol_bridge }}.
|
2025-12-21 18:23:12 +01:00
|
|
|
Please set wol_interface explicitly.
|
2025-12-23 21:11:21 +01:00
|
|
|
Bridge output:
|
|
|
|
|
{{ bridge_links.stdout_lines }}
|
2025-12-23 20:42:03 +01:00
|
|
|
when: wol_detected_phy | default('') | length == 0
|
2025-12-21 18:23:12 +01:00
|
|
|
|
2025-12-23 20:42:03 +01:00
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
# Validate WOL capability
|
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
- name: Check WOL capability on detected NIC
|
|
|
|
|
ansible.builtin.command: "ethtool {{ wol_detected_phy }}"
|
2025-12-14 21:28:45 +01:00
|
|
|
register: wol_capabilities
|
|
|
|
|
changed_when: false
|
|
|
|
|
|
|
|
|
|
- name: Fail if NIC does not support Wake-on-LAN
|
|
|
|
|
ansible.builtin.fail:
|
2025-12-23 20:42:03 +01:00
|
|
|
msg: "Interface {{ wol_detected_phy }} does not support Wake-on-LAN."
|
|
|
|
|
when: "'Supports Wake-on: g' not in wol_capabilities.stdout"
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
# Enable WOL immediately
|
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
- name: Enable Wake-on-LAN immediately
|
|
|
|
|
ansible.builtin.command: >
|
|
|
|
|
ethtool -s {{ wol_detected_phy }} wol {{ wol_mode }}
|
|
|
|
|
changed_when: false
|
2025-12-14 21:28:45 +01:00
|
|
|
|
2025-12-23 20:42:03 +01:00
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
# Persist WOL via udev (correct + recommended)
|
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
- name: Create udev rule to persist Wake-on-LAN
|
|
|
|
|
ansible.builtin.copy:
|
|
|
|
|
dest: /etc/udev/rules.d/90-wol.rules
|
2025-12-14 20:46:32 +01:00
|
|
|
owner: root
|
|
|
|
|
group: root
|
|
|
|
|
mode: '0644'
|
2025-12-23 20:42:03 +01:00
|
|
|
content: |
|
|
|
|
|
ACTION=="add", SUBSYSTEM=="net", KERNEL=="{{ wol_detected_phy }}", RUN+="/sbin/ethtool -s {{ wol_detected_phy }} wol {{ wol_mode }}"
|
|
|
|
|
|
|
|
|
|
- name: Reload udev rules
|
|
|
|
|
ansible.builtin.command: udevadm control --reload
|
|
|
|
|
changed_when: false
|
2025-12-14 20:46:32 +01:00
|
|
|
|
2025-12-23 20:42:03 +01:00
|
|
|
- name: Trigger udev for network interfaces
|
|
|
|
|
ansible.builtin.command: udevadm trigger --subsystem-match=net
|
2025-12-14 20:46:32 +01:00
|
|
|
changed_when: false
|
|
|
|
|
|
2025-12-23 20:42:03 +01:00
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
# Verification & Reporting
|
|
|
|
|
# ------------------------------------------------------------
|
2025-12-14 20:46:32 +01:00
|
|
|
- name: Verify Wake-on-LAN status
|
2025-12-23 20:42:03 +01:00
|
|
|
ansible.builtin.command: "ethtool {{ wol_detected_phy }}"
|
2025-12-14 20:46:32 +01:00
|
|
|
register: wol_status
|
|
|
|
|
changed_when: false
|
|
|
|
|
when: wol_verify
|
|
|
|
|
|
2025-12-23 20:42:03 +01:00
|
|
|
- name: Get MAC address
|
|
|
|
|
ansible.builtin.set_fact:
|
|
|
|
|
wol_mac_address: "{{ ansible_facts.interfaces[wol_detected_phy].macaddress | default('') }}"
|
2025-12-14 21:28:45 +01:00
|
|
|
|
|
|
|
|
- name: Fail if MAC address could not be detected
|
2025-12-23 20:42:03 +01:00
|
|
|
ansible.builtin.fail:
|
|
|
|
|
msg: "Unable to determine MAC address for interface {{ wol_detected_phy }}"
|
2025-12-14 21:28:45 +01:00
|
|
|
when:
|
|
|
|
|
- wol_report_mac
|
|
|
|
|
- wol_mac_address | length == 0
|
|
|
|
|
|
2025-12-23 20:42:03 +01:00
|
|
|
- name: Show WOL status
|
|
|
|
|
ansible.builtin.debug:
|
|
|
|
|
msg: "{{ wol_status.stdout_lines }}"
|
|
|
|
|
when: wol_verify
|
|
|
|
|
|
2025-12-14 21:28:45 +01:00
|
|
|
- name: Report Wake-on-LAN sender details
|
|
|
|
|
ansible.builtin.debug:
|
|
|
|
|
msg:
|
2025-12-23 20:42:03 +01:00
|
|
|
- "Wake-on-LAN enabled successfully"
|
|
|
|
|
- "Bridge: {{ wol_bridge }}"
|
|
|
|
|
- "Physical NIC: {{ wol_detected_phy }}"
|
2025-12-14 21:28:45 +01:00
|
|
|
- "MAC address: {{ wol_mac_address }}"
|
2025-12-23 20:42:03 +01:00
|
|
|
- "Example commands:"
|
2025-12-14 21:28:45 +01:00
|
|
|
- " wakeonlan {{ wol_mac_address }}"
|
|
|
|
|
- " etherwake {{ wol_mac_address }}"
|
|
|
|
|
- "Home Assistant:"
|
|
|
|
|
- " mac: {{ wol_mac_address }}"
|
|
|
|
|
when: wol_report_mac
|