From 942625f89462015d783bae3737675ec457f9edab Mon Sep 17 00:00:00 2001 From: Jose Date: Fri, 26 Dec 2025 10:41:25 +0100 Subject: [PATCH] =?UTF-8?q?refactor=20=E2=99=BB=EF=B8=8F:=20Refactor=20tas?= =?UTF-8?q?k=20to=20use=20template=20for=20creating=20systemd=20service.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit refactors the existing task to utilize a template for creating systemd services, enhancing modularity and maintainability. --- tasks/main.yml | 29 ++++++----------------------- templates/wol-interfaces.service.j2 | 13 +++++++++++++ 2 files changed, 19 insertions(+), 23 deletions(-) create mode 100644 templates/wol-interfaces.service.j2 diff --git a/tasks/main.yml b/tasks/main.yml index 603e03f..5cf0707 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -77,30 +77,13 @@ # ------------------------- # 4. Create systemd service for persistence # ------------------------- -- name: Create systemd service to persist WoL - ansible.builtin.copy: +- name: Create systemd service from template + ansible.builtin.template: + src: wol-interfaces.service.j2 dest: /etc/systemd/system/wol-interfaces.service - content: | - [Unit] - Description=Set Wake-on-LAN on network interfaces - After=network.target - Wants=network.target - - [Service] - Type=oneshot - ExecStart=/bin/bash -c ' - {% for intf, enabled, supported in en_interfaces | zip(wol_enabled.results, wol_supported.results) %} - {% if wol_mode in supported.stdout %} - {% if wol_mode not in enabled.stdout %} - /sbin/ethtool -s {{ intf }} wol {{ wol_mode }}; - {% endif %} - {% endif %} - {% endfor %} - ' - RemainAfterExit=yes - - [Install] - WantedBy=multi-user.target + owner: root + group: root + mode: '0644' notify: - Reload systemd diff --git a/templates/wol-interfaces.service.j2 b/templates/wol-interfaces.service.j2 new file mode 100644 index 0000000..2523e23 --- /dev/null +++ b/templates/wol-interfaces.service.j2 @@ -0,0 +1,13 @@ +[Unit] +Description=Set Wake-on-LAN on network interfaces +After=network.target +Wants=network.target + +[Service] +Type=oneshot +ExecStart=/bin/bash -c '{% for intf, enabled, supported in en_interfaces | zip(wol_enabled.results, wol_supported.results) %} +{% if wol_mode in supported.stdout and wol_mode not in enabled.stdout %}/sbin/ethtool -s {{ intf }} wol {{ wol_mode }}; {% endif %}{% endfor %}' +RemainAfterExit=yes + +[Install] +WantedBy=multi-user.target