refactor ♻️: Refactor handler to reload systemd and restart WOL service.

This commit refactors the handler to include functionality for reloading systemd and restarting the Wake-on-LAN (WOL) service, improving system management capabilities.
This commit is contained in:
2025-12-21 21:40:27 +01:00
parent 7f86cae2ed
commit 724d67982a
2 changed files with 52 additions and 24 deletions

View File

@@ -1,9 +1,7 @@
--- ---
- name: Reload systemd - name: Reload systemd and restart WOL
command: systemctl daemon-reexec ansible.builtin.systemd:
name: wol
- name: Enable and start WOL service daemon_reload: true
systemd: enabled: true
name: wol.service state: restarted
enabled: yes
state: started

View File

@@ -15,21 +15,32 @@
wol_detected_interface: "{{ ansible_default_ipv4.interface }}" wol_detected_interface: "{{ ansible_default_ipv4.interface }}"
when: wol_interface | default('') | length == 0 when: wol_interface | default('') | length == 0
- name: Debug facts interface
ansible.builtin.debug:
msg:
- "Detected bridge {{ wol_detected_interface }}"
- name: Debug facts - name: Debug facts
ansible.builtin.debug: ansible.builtin.debug:
msg: msg:
- "{{ hostvars[inventory_hostname] }}" - "{{ 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 enslaved to bridge
ansible.builtin.set_fact:
wol_detected_phy: "{{ item }}"
loop: "{{ ansible_interfaces }}"
when:
- wol_interface | default('') | length == 0
- item != 'lo'
- hostvars[inventory_hostname]['ansible_' + item] is defined
- hostvars[inventory_hostname]['ansible_' + item].master is defined
- hostvars[inventory_hostname]['ansible_' + item].master == wol_detected_bridge
# - name: Detect physical NIC behind bridge # - name: Detect physical NIC behind bridge
# ansible.builtin.set_fact: # ansible.builtin.set_fact:
@@ -40,12 +51,25 @@
# - ansible_facts[wol_detected_bridge].interfaces is defined # - ansible_facts[wol_detected_bridge].interfaces is defined
# - ansible_facts[wol_detected_bridge].interfaces | length > 0 # - 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_bridge].interfaces
| select('match', '^(e|en)')
| list
}}
- name: Debug WoL interface selection - name: Debug WoL interface selection
ansible.builtin.debug: ansible.builtin.debug:
msg: msg:
- "Detected interface: {{ wol_detected_interface }}" - "Detected interface: {{ wol_detected_interface }}"
- "Detected physical NIC: {{ wol_detected_phy }}" - "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 - name: Select final WoL interface
ansible.builtin.set_fact: ansible.builtin.set_fact:
@@ -56,6 +80,14 @@
else wol_detected_phy 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 - name: Fail if no physical NIC was detected
ansible.builtin.fail: ansible.builtin.fail:
msg: > msg: >
@@ -72,8 +104,7 @@
- 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_final_interface }} 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: "'Wake-on:' not in wol_capabilities.stdout"
'Wake-on:' not in wol_capabilities.stdout"
- name: Create systemd service for Wake-on-LAN - name: Create systemd service for Wake-on-LAN
ansible.builtin.template: ansible.builtin.template:
@@ -83,8 +114,7 @@
group: root group: root
mode: '0644' mode: '0644'
notify: notify:
- Reload systemd - Reload systemd and restart WOL
- Enable and start WOL service
- name: Enable WOL immediately (without reboot) - name: Enable WOL immediately (without reboot)
ansible.builtin.command: ethtool -s {{ wol_final_interface }} wol {{ wol_mode }} ansible.builtin.command: ethtool -s {{ wol_final_interface }} wol {{ wol_mode }}