refactor ♻️: Simplify task logic by removing redundant debug outputs and combining checks.
Some checks failed
ansible-lint / Ansible Lint (push) Failing after 9s

Refactored the task processing logic to eliminate unnecessary debug statements and consolidate related conditional checks for improved readability and efficiency.
This commit is contained in:
2025-12-26 09:08:02 +01:00
parent 2dc6fd8158
commit 39430b88f3

View File

@@ -40,9 +40,17 @@
msg: > msg: >
{{ en_interfaces }} {{ en_interfaces }}
- name: Display var selected interfaces - name: Check supported Wake-on-LAN modes
ansible.builtin.debug: ansible.builtin.shell: "ethtool {{ item }} | grep 'Supports Wake-on' | tail -1 | awk '{print $3}'"
var: en_interfaces loop: "{{ en_interfaces }}"
register: wol_supported
changed_when: false
when: en_interfaces | length > 0
# - name: Display supported WoL modes
# ansible.builtin.debug:
# msg: "Interface {{ item.item }} supports WoL modes: {{ item.stdout }}"
# loop: "{{ wol_supported.results }}"
- name: WOL | Check if enabled - name: WOL | Check if enabled
shell: > shell: >
@@ -53,83 +61,18 @@
loop: "{{ en_interfaces }}" loop: "{{ en_interfaces }}"
when: en_interfaces | length > 0 when: en_interfaces | length > 0
- name: Display WoL status # - name: Display WoL status
ansible.builtin.debug: # ansible.builtin.debug:
msg: "Interface {{ item.item }} WoL status: {{ item.stdout }}" # msg: "Interface {{ item.item }} WoL status: {{ item.stdout }}"
loop: "{{ wol_enabled.results }}" # loop: "{{ wol_enabled.results }}"
- name: Check supported Wake-on-LAN modes
ansible.builtin.shell: "ethtool {{ item }} | grep 'Supports Wake-on' | tail -1 | awk '{print $3}'" - name: Enable Wake-on-LAN (magic packet) when supported
ansible.builtin.command: "ethtool -s {{ item }} wol g"
when:
- g not in wol_enabled.results.stdout
- g in wol_supported.results.stdout
loop: "{{ en_interfaces }}" loop: "{{ en_interfaces }}"
register: wol_supported
changed_when: false
when: en_interfaces | length > 0
- name: Display supported WoL modes
ansible.builtin.debug:
msg: "Interface {{ item.item }} supports WoL modes: {{ item.stdout }}"
loop: "{{ wol_supported.results }}"
- name: Check WoL status with ethtool
ansible.builtin.shell: "ethtool {{ item }} | grep 'Wake-on'"
register: wol_status
changed_when: false
failed_when: false
loop: "{{ en_interfaces }}"
when: en_interfaces | length > 0
- name: Display var wol_status
ansible.builtin.debug:
var: wol_status
- name: Extract Supports Wake-on per interface
ansible.builtin.set_fact:
supports_wake_on: >-
{{
supports_wake_on | default({}) |
combine({
item.item:
(item.stdout | regex_search('Supports Wake-on:\\s*(\\S+)', '\\1'))
})
}}
loop: "{{ wol_status.results }}"
when: item.stdout is defined
# - name: Extract Supports Wake-on value
# set_fact:
# supports_wake_on: "{{ wol_status.results | regex_search('(?m)^\\s*Supports Wake-on:\\s*(\\S+)', '\\1') }}"
- name: Show Supports Wake-on value
debug:
msg: "Supports Wake-on is: {{ supports_wake_on }}"
- name: Extract Wake-on value
set_fact:
wake_on: "{{ wol_status.results | regex_search('(?m)^\\s*Wake-on:\\s*(\\S+)', '\\1') }}"
- name: Show Wake-on value
debug:
msg: "Wake-on is: {{ wake_on }}"
- name: Display WoL status
ansible.builtin.debug:
msg: "{{ item.item }}:\n{{ item.stdout | default('No output') }}"
loop: "{{ wol_status.results }}"
when: item.stdout is defined
# - name: Enable Wake-on-LAN (magic packet) when supported
# ansible.builtin.command: "ethtool -s {{ item.key }} wol g"
# when:
# - item.value.supports_magic
# - item.value.current_state != 'g'
# loop: "{{ wol_info | dict2items }}"
# # ============================================================ # # ============================================================