diff --git a/handlers/main.yml b/handlers/main.yml index a3d4555..714e830 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,9 +1,7 @@ --- -- name: Reload systemd - command: systemctl daemon-reexec - -- name: Enable and start WOL service - systemd: - name: wol.service - enabled: yes - state: started +- name: Reload systemd and restart WOL + ansible.builtin.systemd: + name: wol + daemon_reload: true + enabled: true + state: restarted diff --git a/tasks/main.yml b/tasks/main.yml index 59e5c11..b1d1c5c 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -15,21 +15,32 @@ 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] }}" + - "{{ 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 # ansible.builtin.set_fact: @@ -40,12 +51,25 @@ # - 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_bridge].interfaces + | select('match', '^(e|en)') + | list + }} - name: Debug WoL interface selection ansible.builtin.debug: msg: - "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 ansible.builtin.set_fact: @@ -56,6 +80,14 @@ 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: > @@ -72,8 +104,7 @@ - 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: "'Supports Wake-on-Wake-on-LAN' in wol_capabilities.stdout or - 'Wake-on:' not in wol_capabilities.stdout" + when: "'Wake-on:' not in wol_capabilities.stdout" - name: Create systemd service for Wake-on-LAN ansible.builtin.template: @@ -83,8 +114,7 @@ group: root mode: '0644' notify: - - Reload systemd - - Enable and start WOL service + - Reload systemd and restart WOL - name: Enable WOL immediately (without reboot) ansible.builtin.command: ethtool -s {{ wol_final_interface }} wol {{ wol_mode }}