Files
Kamino/roles/systemd_hardening/molecule/default/prepare.yml
T
Dragonish f256c1c7e5
Build / build (pull_request) Successful in 18s
Lint / lint (pull_request) Successful in 1m11s
Molecule / molecule (pull_request) Successful in 1m2s
fix(systemd_hardening): disable and mask the socket of a listed service too
Look up every listed name as a socket and as a service and act on each
installed unit, the socket first. A service whose socket stays active is
started again by the next connection to its port.

Assisted-by: Claude Code
2026-11-23 11:18:49 +00:00

61 lines
2.1 KiB
YAML

---
# The image installs none of the services the role touches, so the units it acts on are
# stubs in /usr/lib/systemd/system (not /etc: `systemctl mask` refuses to replace a regular
# file there). rpcbind and telnet are a socket and a service each, as the real packages ship them;
# cups has only a service and bluetooth only a socket (its Service= is a stub of another name:
# systemd refuses to start a socket whose service is not loaded), so the role meets every shape. The sockets
# listen on a unix socket under /run and are started, so the role has something to stop. rpcbind,
# cups and bluetooth are enabled so the role has something to disable; telnet only needs to
# exist; testsvc receives the drop-in.
- name: Prepare
hosts: all
gather_facts: false
tasks:
- name: Install the stub unit files
ansible.builtin.copy:
dest: "/usr/lib/systemd/system/{{ item }}.service"
content: |
[Unit]
Description=molecule stub for {{ item }}
[Service]
ExecStart=/bin/sleep 3600
[Install]
WantedBy=multi-user.target
owner: root
group: root
mode: "0644"
loop: [rpcbind, telnet, cups, testsvc, molecule-bluetooth-target]
- name: Install the stub socket unit files
ansible.builtin.copy:
dest: "/usr/lib/systemd/system/{{ item }}.socket"
content: |
[Unit]
Description=molecule stub socket for {{ item }}
[Socket]
ListenStream=/run/molecule-{{ item }}.sock
{% if item == 'bluetooth' %}
Service=molecule-bluetooth-target.service
{% endif %}
[Install]
WantedBy=sockets.target
owner: root
group: root
mode: "0644"
loop: [rpcbind, telnet, bluetooth]
- name: Reload systemd
ansible.builtin.systemd:
daemon_reload: true
- name: Enable and start the stub sockets and services
ansible.builtin.systemd:
name: "{{ item }}"
enabled: true
state: started
loop: [rpcbind.socket, telnet.socket, bluetooth.socket, rpcbind.service, cups.service, telnet.service]