Files
ansible_proxmox_WOL/tasks/main.yml
Jose c5a1346eea
Some checks failed
ansible-lint / Ansible Lint (push) Failing after 11s
docs 📝: Update README.md with usage examples, scenario descriptions, and known issues
This commit updates the README.md file to include a new section on 'Usage Examples' with detailed examples of how to use the role in different scenarios. It also updates the 'Common Proxmox Scenarios' table with more detailed descriptions and adds a new section on 'Known Issues'.
2025-12-26 16:03:19 +01:00

109 lines
3.3 KiB
YAML

---
# ============================================================
# Install required packages
# ============================================================
- name: Install required packages
ansible.builtin.apt:
name: ethtool
state: present
update_cache: true
# ============================================================
# Detect physical NICs with WOL support using Ansible facts
# ============================================================
- name: Gather network interface facts
ansible.builtin.setup:
gather_subset:
- network
when: ansible_facts.interfaces is not defined
- name: Display interfaces
ansible.builtin.debug:
msg: >
{{ ansible_facts.interfaces }}
# - name: Get interfaces starting with "en" or "eth"
# ansible.builtin.set_fact:
# en_interfaces: "{{ ansible_facts.interfaces | select('match', '^eth|^ens|^enp') | unique | list }}"
- name: Get interfaces starting with "en or "eth"
ansible.builtin.set_fact:
en_interfaces: >-
{{
ansible_facts.interfaces
| select('match', '^(eth|en)')
| list
}}
- name: Display debug selected interfaces
ansible.builtin.debug:
msg: >
{{ en_interfaces }}
- name: Check supported Wake-on-LAN modes
ansible.builtin.shell: "ethtool {{ item }} | grep 'Supports Wake-on' | tail -1 | awk '{print $3}'"
loop: "{{ en_interfaces }}"
register: wol_supported
changed_when: false
when: en_interfaces | length > 0
- name: WOL | Check if enabled
shell: >
ethtool {{ item }} | grep 'Wake-on' | tail -1 | awk '{print substr($0,length,1)}'
register: wol_enabled
changed_when: false
failed_when: false
loop: "{{ en_interfaces }}"
when: en_interfaces | length > 0
- name: "Set Wake-on-LAN to {{ wol_mode }}"
ansible.builtin.command: "ethtool -s {{ item.0 }} wol {{ wol_mode }}"
loop: "{{ en_interfaces | zip(wol_enabled.results, wol_supported.results) | list }}"
loop_control:
label: "{{ item.0 }}"
when:
- wol_mode not in item.1.stdout
- wol_mode in item.2.stdout
- name: "Disable Wake-on-LAN"
ansible.builtin.command: "ethtool -s {{ item.0 }} wol {{ wol_mode }}"
loop: "{{ en_interfaces | zip(wol_enabled.results, wol_supported.results) | list }}"
loop_control:
label: "{{ item.0 }}"
when:
- wol_mode == 'd'
- wol_mode not in item.1.stdout
- name: Deploy wol systemd template
ansible.builtin.template:
src: templates/wol@.service.j2
dest: /etc/systemd/system/wol@.service
mode: '0644'
notify: Reload systemd
when: en_interfaces | length > 0
- name: Enable WOL systemd unit for each interface
ansible.builtin.systemd:
name: "wol@{{ item }}.service"
enabled: true
state: started
loop: "{{ en_interfaces }}"
when: en_interfaces | length > 0
- name: Get MAC addresses
ansible.builtin.set_fact:
wol_mac_addresses: >-
{{ wol_mac_addresses | default([]) + [ hostvars[inventory_hostname]['ansible_' ~ item].macaddress ] }}
loop: "{{ en_interfaces }}"
when: en_interfaces | length > 0
- name: Report WOL configuration
ansible.builtin.debug:
msg: |
Wake-on-LAN Configuration Summary:
===================================
Physical Interfaces: {{ en_interfaces | join(', ') }}
WOL Mode: {{ wol_mode }}
MAC Addresses: {{ wol_mac_addresses | join(', ') }}