Files
ansible_proxmox_WOL/tasks/main.yml

161 lines
4.8 KiB
YAML
Raw Normal View History

---
- name: Install required packages
ansible.builtin.apt:
name: ethtool
state: present
update_cache: yes
- name: Gather network facts
ansible.builtin.setup:
gather_subset:
- network
- name: Detect default route interface
ansible.builtin.set_fact:
wol_detected_interface: "{{ ansible_default_ipv4.interface }}"
when: wol_interface | default('') | length == 0
- name: Debug facts interface
ansible.builtin.debug:
msg:
- "Detected bridge {{ wol_detected_interface }}"
# - name: Debug facts
# ansible.builtin.debug:
# msg:
# - "{{ hostvars[inventory_hostname]['ansible_' + wol_detected_interface][interfaces] }}"
# - name: Detect physical NIC enslaved to bridge
# ansible.builtin.set_fact:
# wol_detected_phy: "{{ item }}"
# loop: "{{ ansible_interfaces }}"
# when:
# - wol_interface | default('') | length == 0
# - item != 'lo'
# - item is not match('^v')
# - item is match('^e')
# - hostvars[inventory_hostname]['ansible_' + item] is defined
# - hostvars[inventory_hostname]['ansible_' + item].module is defined
# - hostvars[inventory_hostname]['ansible_' + item].pciid is defined
# - hostvars[inventory_hostname]['ansible_' + item].type is defined
# - hostvars[inventory_hostname]['ansible_' + item].type == 'ether'
# - name: Detect physical NIC behind bridge
# ansible.builtin.set_fact:
# wol_detected_phy: "{{ ansible_facts[wol_detected_bridge].interfaces[0] }}"
# when:
# - wol_interface | default('') | length == 0
# - ansible_facts[wol_detected_bridge] is defined
# - ansible_facts[wol_detected_bridge].interfaces is defined
# - ansible_facts[wol_detected_bridge].interfaces | length > 0
- name: Detect physical NICs in bridge
ansible.builtin.set_fact:
wol_bridge_phys: >-
{{
ansible_facts[wol_detected_interface].interfaces
| select('match', '^(e|en)')
| list
}}
- name: Debug WoL interface selection
ansible.builtin.debug:
msg:
- "Detected interface: {{ wol_detected_interface }}"
- "Detected physical NICs: {{ wol_detected_phys }}"
- name: Select physical NIC if exactly one found
ansible.builtin.set_fact:
wol_detected_phy: "{{ wol_bridge_phys[0] }}"
when: wol_bridge_phys | length == 1
- name: Select final WoL interface
ansible.builtin.set_fact:
wol_final_interface: >-
{{
wol_interface
if wol_interface | default('') | length > 0
else wol_detected_phy
}}
- name: Fail if multiple NICs are attached to bridge
ansible.builtin.fail:
msg: >
Multiple physical NICs found in {{ wol_detected_bridge }}:
{{ wol_bridge_phys }}.
Please set wol_interface explicitly.
when: wol_bridge_phys | length > 1
- name: Fail if no physical NIC was detected
ansible.builtin.fail:
msg: >
Unable to detect a physical NIC for Wake-on-LAN.
Detected bridge: {{ wol_detected_bridge | default('none') }}.
Please set wol_interface explicitly.
when: wol_final_interface | default('') | length == 0
- name: Check WOL support on interface
ansible.builtin.command: ethtool {{ wol_final_interface }}
register: wol_capabilities
changed_when: false
- name: Fail if NIC does not support Wake-on-LAN
ansible.builtin.fail:
msg: "Interface {{ wol_final_interface }} does not support Wake-on-LAN."
when: "'Wake-on:' not in wol_capabilities.stdout"
- name: Create systemd service for Wake-on-LAN
ansible.builtin.template:
src: wol.service.j2
dest: /etc/systemd/system/wol.service
owner: root
group: root
mode: '0644'
notify:
- Reload systemd and restart WOL
- name: Enable WOL immediately (without reboot)
ansible.builtin.command: ethtool -s {{ wol_final_interface }} wol {{ wol_mode }}
changed_when: false
- name: Verify Wake-on-LAN status
ansible.builtin.command: ethtool {{ wol_final_interface }}
register: wol_status
changed_when: false
when: wol_verify
- name: Get MAC address of WOL interface
set_fact:
wol_mac_address: >-
{{
ansible_facts['ansible_' + wol_final_interface]['macaddress']
| default('')
}}
- name: Show WOL status
ansible.builtin.debug:
msg: "{{ wol_status.stdout_lines }}"
when: wol_verify
- name: Fail if MAC address could not be detected
fail:
msg: "Unable to determine MAC address for interface {{ wol_final_interface }}"
when:
- wol_report_mac
- wol_mac_address | length == 0
- name: Report Wake-on-LAN sender details
ansible.builtin.debug:
msg:
- "Wake-on-LAN enabled on interface: {{ wol_final_interface }}"
- "MAC address: {{ wol_mac_address }}"
- "Example WOL commands:"
- " wakeonlan {{ wol_mac_address }}"
- " etherwake {{ wol_mac_address }}"
- "Home Assistant:"
- " mac: {{ wol_mac_address }}"
when: wol_report_mac