From cf2286b7a7c5a7c651db9df2ac558ad72736fb7f Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Thu, 26 Nov 2026 10:11:54 +0000 Subject: [PATCH 1/3] test(roles): run molecule's check step before and after converge Every role scenario with a converge now runs check right before it (a fresh host) and right after it (a configured host), and a script test fails a scenario without both. pihole and pve_lxc move their second check from after idempotence to after converge to match. The roles that failed under --check on a fresh host get the two allowed guards: cloudflared, docker, gitea, gitea_runner, traefik and user_management. ufw and fail2ban preview only a host where their package is installed, as their READMEs now say. Closes #134 Assisted-by: Claude Code --- .gitea/workflows/AGENTS.md | 7 ++++--- changelogs/fragments/134-check-mode.yml | 17 +++++++++++++++++ roles/AGENTS.md | 5 +++-- roles/_binary_update/README.md | 3 ++- .../molecule/default/molecule.yml | 3 +++ roles/cloudflared/README.md | 3 ++- roles/cloudflared/handlers/main.yml | 2 ++ roles/cloudflared/molecule/default/molecule.yml | 3 +++ roles/cloudflared/tasks/configure.yml | 8 +++++++- roles/cloudflared/tasks/prereqs.yml | 4 ++++ roles/cloudflared/tasks/update.yml | 4 +++- roles/docker/README.md | 3 ++- roles/docker/molecule/default/molecule.yml | 3 +++ roles/docker/tasks/install.yml | 6 ++++++ roles/docker/tasks/main.yml | 7 ++++++- roles/fail2ban/README.md | 3 ++- roles/fail2ban/molecule/default/molecule.yml | 3 +++ roles/gitea/README.md | 3 ++- roles/gitea/handlers/main.yml | 2 ++ roles/gitea/molecule/default/molecule.yml | 3 +++ roles/gitea/tasks/configure.yml | 11 ++++++++++- roles/gitea/tasks/install.yml | 2 ++ roles/gitea/tasks/update.yml | 4 +++- roles/gitea_runner/README.md | 3 ++- roles/gitea_runner/handlers/main.yml | 2 ++ .../gitea_runner/molecule/default/molecule.yml | 3 +++ roles/gitea_runner/tasks/configure.yml | 12 +++++++++++- roles/gitea_runner/tasks/docker.yml | 3 +++ roles/gitea_runner/tasks/prereqs.yml | 2 ++ roles/gitea_runner/tasks/update.yml | 4 +++- roles/network_interfaces/README.md | 3 ++- .../molecule/default/molecule.yml | 3 +++ roles/pihole/README.md | 2 +- roles/pihole/molecule/default/molecule.yml | 4 ++-- roles/pve_lxc/README.md | 2 +- roles/pve_lxc/molecule/default/molecule.yml | 4 ++-- roles/sshd/README.md | 3 ++- roles/sshd/molecule/default/molecule.yml | 3 +++ roles/system_baseline/README.md | 4 +++- .../molecule/default/molecule.yml | 3 +++ roles/systemd_hardening/README.md | 2 +- .../molecule/default/molecule.yml | 3 +++ roles/traefik/README.md | 3 ++- roles/traefik/handlers/main.yml | 2 ++ roles/traefik/molecule/default/molecule.yml | 3 +++ roles/traefik/tasks/binary/configure.yml | 6 +++++- roles/traefik/tasks/binary/install.yml | 5 +++++ roles/traefik/tasks/main.yml | 2 ++ roles/ufw/README.md | 5 +++-- roles/ufw/molecule/default/converge.yml | 4 ++-- roles/ufw/molecule/default/molecule.yml | 3 +++ .../ufw/molecule/default/tasks/orphan_check.yml | 2 ++ roles/user_management/README.md | 3 ++- .../molecule/default/molecule.yml | 3 +++ roles/user_management/tasks/ssh_keys.yml | 7 +++++++ tests/scripts/test_run_molecule_scenarios.py | 15 +++++++++++++++ 56 files changed, 202 insertions(+), 35 deletions(-) create mode 100644 changelogs/fragments/134-check-mode.yml diff --git a/.gitea/workflows/AGENTS.md b/.gitea/workflows/AGENTS.md index 3eb7b66..38c3a8a 100644 --- a/.gitea/workflows/AGENTS.md +++ b/.gitea/workflows/AGENTS.md @@ -110,14 +110,15 @@ issue. *Why:* a pull request that raises the ansible-core floor must run its scenarios on the new core before it automerges. *Held:* control: the pin comparison in `molecule.yml`. - **Every scenario sets `scenario.test_sequence` to molecule's default minus both `destroy` - steps, names each platform `${KAMINO_RUN_ID:-local}-` and labels it + steps, with a role scenario's `check` right before and right after `converge`, names each platform `${KAMINO_RUN_ID:-local}-` and labels it `net.rkdr.mortis.molecule-run: "${KAMINO_RUN_ID:-local}"` in its `molecule.yml`;** copy it into a new scenario. *Why:* the run script removes each scenario's containers by those names as soon as it ends, which skips the driver's destroy wait, the workflow finds a run's containers by the label, and the molecule state lives in each scenario's own `ANSIBLE_HOME` in the job tree, so a killed run leaves none behind. *Held:* control: `tests/scripts/test_run_molecule_scenarios.py` - fails a `destroy` in a sequence, a platform name that is not unique or not per run, a missing - label, and a workflow that does not filter by it. + fails a `destroy` in a sequence, a role scenario's `converge` without a `check` on each side, a + platform name that is not unique or not per run, a missing label, and a workflow that does not + filter by it. - **Scenarios live in `roles/*/molecule/*/` and `extensions/molecule/*/`.** A role scenario that must pass on Ubuntu 24.04 and 26.04 takes `image: ${MOLECULE_IMAGE}` or `${MOLECULE_IMAGE_2604}` per platform; the image-smoke scenarios are described in diff --git a/changelogs/fragments/134-check-mode.yml b/changelogs/fragments/134-check-mode.yml new file mode 100644 index 0000000..b10398a --- /dev/null +++ b/changelogs/fragments/134-check-mode.yml @@ -0,0 +1,17 @@ +--- +bugfixes: + - >- + cloudflared - Do not fail under ``--check`` on a host where the cloudflared account or systemd + unit does not exist yet. + - >- + docker - Do not fail under ``--check`` on a host where the Docker repository and packages are + not installed yet. + - >- + gitea - Do not fail under ``--check`` on a host where Gitea is not installed yet. + - >- + gitea_runner - Do not fail under ``--check`` on a host where the runner account, Docker or the + systemd unit does not exist yet. + - >- + traefik - Do not fail under ``--check`` on a host where Traefik is not installed yet. + - >- + user_management - Do not fail under ``--check`` when a managed user does not exist yet. diff --git a/roles/AGENTS.md b/roles/AGENTS.md index cf25c04..a836163 100644 --- a/roles/AGENTS.md +++ b/roles/AGENTS.md @@ -99,8 +99,9 @@ names a check that does not exist yet and its backlog issue. `check_mode: false` on read-only probes and `when: not ansible_check_mode`; `--check` is the supported preview, and the README says a play-level `check_mode: true` is not supported. A role that cannot support check mode says why in README Notes and drops the step. *Why:* check-mode - breakage is a frequent fix, and one guard pattern is reviewable. *Held:* review until every - scenario's `test_sequence` runs `check` (#134). + breakage is a frequent fix, and one guard pattern is reviewable. *Held:* Molecule for the run + passing, `tests/scripts/test_run_molecule_scenarios.py` for the steps being there; review for + the guards. ## Roles calling roles diff --git a/roles/_binary_update/README.md b/roles/_binary_update/README.md index 5c555d9..1b0cfeb 100644 --- a/roles/_binary_update/README.md +++ b/roles/_binary_update/README.md @@ -71,7 +71,8 @@ Two molecule scenarios run in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ub stub unit, with no network: `default` covers a successful bump, its idempotence, a binary that cannot start, a binary that reports another version (both rolled back), a run with nothing staged (the service is not stopped), a direct call from a play (refused) and a first install -with nothing to back up. `check_mode` runs the bump under `--check` against a stopped service +with nothing to back up, and runs `check` before the converge (on a fresh host) and after it +(on the configured host). `check_mode` runs the bump under `--check` against a stopped service and checks that nothing changed. The calling roles' own scenarios cover a real upstream binary. Idempotent: yes. diff --git a/roles/_binary_update/molecule/default/molecule.yml b/roles/_binary_update/molecule/default/molecule.yml index 5212e37..92ea0cd 100644 --- a/roles/_binary_update/molecule/default/molecule.yml +++ b/roles/_binary_update/molecule/default/molecule.yml @@ -46,13 +46,16 @@ verifier: scenario: # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. + # A check runs before the converge (fresh host) and after it (configured host). test_sequence: - dependency - cleanup - syntax - create - prepare + - check - converge + - check - idempotence - side_effect - verify diff --git a/roles/cloudflared/README.md b/roles/cloudflared/README.md index b1c432f..2909705 100644 --- a/roles/cloudflared/README.md +++ b/roles/cloudflared/README.md @@ -106,7 +106,7 @@ Remove with `-e "cloudflared_remove=true cloudflared_remove_data=true"`; only `c ## Tests -The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It runs without Cloudflare: a stub cloudflared binary stands in for the daemon, the downloads come from local `file://` URLs, and a stub of the Cloudflare API on the controller's loopback (`molecule/default/files/cloudflare_api_stub.py`, reached through `cloudflared_api_base_url`) answers the tunnel, zone and DNS record calls. It covers the converge with `cloudflared_manage_dns` false and an update through `_binary_update` (and its idempotence), the rendered `config.yml`, credentials file and unit, a stale credentials file, a failed checksum that leaves the daemon running, the input asserts, the deleted-tunnel and DNS guards, the `mint` entry point (confirm guard, CI refusal, no-op, check mode and a mint against the stub, run as child processes) and the remove path. The `community.general.cloudflare_dns` calls, which cannot be pointed at the stub, are not covered: creating, pruning and deleting records. +The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It runs without Cloudflare: a stub cloudflared binary stands in for the daemon, the downloads come from local `file://` URLs, and a stub of the Cloudflare API on the controller's loopback (`molecule/default/files/cloudflare_api_stub.py`, reached through `cloudflared_api_base_url`) answers the tunnel, zone and DNS record calls. It covers the converge with `cloudflared_manage_dns` false and an update through `_binary_update` (and its idempotence), the rendered `config.yml`, credentials file and unit, a stale credentials file, a failed checksum that leaves the daemon running, the input asserts, the deleted-tunnel and DNS guards, the `mint` entry point (confirm guard, CI refusal, no-op, check mode and a mint against the stub, run as child processes) and the remove path. The `community.general.cloudflare_dns` calls, which cannot be pointed at the stub, are not covered: creating, pruning and deleting records. It runs `check` before the converge (a fresh host) and after it (a configured host). ## Notes @@ -120,3 +120,4 @@ The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_I - The `Restart cloudflared` handler runs when `config.yml` or the credentials file changes. cloudflared's own ingress check (`ingress validate`) runs after `config.yml` is written. - Update: the version is compared as `YYYY.M.P` against `cloudflared --version`. The new binary is downloaded and checksum-verified into `cloudflared.new` before the service is stopped, so a failed download leaves it running; a configured hash that equals the installed binary's is refused with a message, since it means the hash was not bumped with the version. A binary that reports no version is replaced too. - The play fails when `cloudflared` is not `active` after start. +- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. Under `--check`, the config directory task is skipped when the `cloudflared` group or user is new. When the systemd unit is new or would change, the start, the running check, the `Restart cloudflared` handler and the update swap are skipped. diff --git a/roles/cloudflared/handlers/main.yml b/roles/cloudflared/handlers/main.yml index 4c6c880..77ae5a4 100644 --- a/roles/cloudflared/handlers/main.yml +++ b/roles/cloudflared/handlers/main.yml @@ -4,3 +4,5 @@ ansible.builtin.systemd: name: cloudflared state: restarted + # Under --check a new unit is only simulated, so systemd would not find it. + when: not ansible_check_mode or __cloudflared_unit_file | default({}) is not changed diff --git a/roles/cloudflared/molecule/default/molecule.yml b/roles/cloudflared/molecule/default/molecule.yml index 9e06b9d..820348b 100644 --- a/roles/cloudflared/molecule/default/molecule.yml +++ b/roles/cloudflared/molecule/default/molecule.yml @@ -90,13 +90,16 @@ verifier: scenario: # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. + # A check runs before the converge (fresh host) and after it (configured host). test_sequence: - dependency - cleanup - syntax - create - prepare + - check - converge + - check - idempotence - side_effect - verify diff --git a/roles/cloudflared/tasks/configure.yml b/roles/cloudflared/tasks/configure.yml index 4478a4b..3fd0c3c 100644 --- a/roles/cloudflared/tasks/configure.yml +++ b/roles/cloudflared/tasks/configure.yml @@ -281,6 +281,7 @@ owner: root group: root mode: "0644" + register: __cloudflared_unit_file - name: Reload systemd daemon ansible.builtin.systemd: @@ -291,6 +292,8 @@ name: cloudflared enabled: true state: started + # Under --check a new unit is only simulated, so systemd would not find it. + when: not ansible_check_mode or __cloudflared_unit_file is not changed - name: Check cloudflared service status ansible.builtin.systemd: @@ -301,5 +304,8 @@ - name: Fail if cloudflared service is not running ansible.builtin.fail: msg: "cloudflared service is not running" - when: __cloudflared_service_status.status.ActiveState != "active" + when: + - __cloudflared_service_status.status.ActiveState != "active" + # The start above is skipped under --check for a new unit. + - not ansible_check_mode or __cloudflared_unit_file | default({}) is not changed tags: [cloudflared, configure, health] diff --git a/roles/cloudflared/tasks/prereqs.yml b/roles/cloudflared/tasks/prereqs.yml index 8865549..dcb31f8 100644 --- a/roles/cloudflared/tasks/prereqs.yml +++ b/roles/cloudflared/tasks/prereqs.yml @@ -4,6 +4,7 @@ ansible.builtin.group: name: "{{ cloudflared_group }}" system: true + register: __cloudflared_group_account tags: [cloudflared, prereqs, always] - name: Create cloudflared user @@ -15,6 +16,7 @@ comment: "Cloudflare Tunnel daemon" system: true create_home: false + register: __cloudflared_user_account tags: [cloudflared, prereqs, always] - name: Create cloudflared config directory @@ -25,6 +27,8 @@ owner: "{{ cloudflared_user }}" group: "{{ cloudflared_group }}" mode: "0750" + # Under --check a new account is only simulated, so an existing directory cannot be chowned to it. + when: not ansible_check_mode or (__cloudflared_group_account is not changed and __cloudflared_user_account is not changed) tags: [cloudflared, prereqs, always] - name: Check if cloudflared binary exists diff --git a/roles/cloudflared/tasks/update.yml b/roles/cloudflared/tasks/update.yml index 8100241..a21a4c3 100644 --- a/roles/cloudflared/tasks/update.yml +++ b/roles/cloudflared/tasks/update.yml @@ -80,7 +80,8 @@ cloudflared was not stopped and still runs {{ __cloudflared_installed_version }}. tags: [cloudflared, update] - # An include does not pass its tags on to the included tasks, hence apply. + # An include does not pass its tags on to the included tasks, hence apply. Under --check a + # new unit is only simulated, so the stop and start of the swap would not find it. - name: Swap in the staged cloudflared binary ansible.builtin.include_role: name: mortis.kamino._binary_update @@ -93,6 +94,7 @@ _binary_update_version_command: ["{{ cloudflared_install_dir }}/cloudflared", --version] _binary_update_expected_version: "{{ cloudflared_version }}" _binary_update_version_regex: '([0-9]{4}\.[0-9]+\.[0-9]+)' + when: not ansible_check_mode or __cloudflared_unit_file | default({}) is not changed tags: [cloudflared, update] always: diff --git a/roles/docker/README.md b/roles/docker/README.md index fb1435f..5c75a89 100644 --- a/roles/docker/README.md +++ b/roles/docker/README.md @@ -86,7 +86,7 @@ With any of `docker_daemon_options`, `docker_live_restore`, `docker_log_driver`, ## Tests -`molecule/default` runs in CI only (`.gitea/workflows/molecule.yml`, no local runner), on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). A real Docker Engine cannot run there: `dockerd` needs the bridge, iptables rules and a delegated cgroup, and the container has no privileged mode. So the scenario builds a local APT repository, signs it with a throwaway key, and points the role at it with `docker_gpg_url` and `docker_repo_base_url`. The repository holds empty stand-ins for `docker-ce`, `docker-ce-cli` and `containerd.io`. The stand-in `docker-ce` ships a stub `docker.service` (`sleep infinity`) and creates the `docker` group, as the real package does. +`molecule/default` runs in CI only (`.gitea/workflows/molecule.yml`, no local runner), on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). A real Docker Engine cannot run there: `dockerd` needs the bridge, iptables rules and a delegated cgroup, and the container has no privileged mode. So the scenario builds a local APT repository, signs it with a throwaway key, and points the role at it with `docker_gpg_url` and `docker_repo_base_url`. The repository holds empty stand-ins for `docker-ce`, `docker-ce-cli` and `containerd.io`. The stand-in `docker-ce` ships a stub `docker.service` (`sleep infinity`) and creates the `docker` group, as the real package does. It runs `check` before the converge (on a fresh host) and after it (on the configured host). What it shows, with the role's tasks running unchanged: @@ -104,6 +104,7 @@ What it does not show: what the units do at a real boot or shutdown (the contain ## Notes - Idempotent: yes. +- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. On a host where the Docker source line is not written yet, the package install, the service start, adding `docker_users` to `docker_group` and the `docker_update` tasks are skipped under `--check`, because the repository they need is only simulated. - `docker_remove` is checked first: when true, only `remove.yml` runs. - Removal with `docker_remove_data` is destructive. It deletes `/var/lib/docker`. - The update task resolves each package's candidate with `apt-cache policy` and fails if a package has none. It installs `package=version`, never `latest`. diff --git a/roles/docker/molecule/default/molecule.yml b/roles/docker/molecule/default/molecule.yml index d7cfe21..351bcb5 100644 --- a/roles/docker/molecule/default/molecule.yml +++ b/roles/docker/molecule/default/molecule.yml @@ -49,13 +49,16 @@ verifier: scenario: # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. + # A check runs before the converge (fresh host) and after it (configured host). test_sequence: - dependency - cleanup - syntax - create - prepare + - check - converge + - check - idempotence - side_effect - verify diff --git a/roles/docker/tasks/install.yml b/roles/docker/tasks/install.yml index 9671588..31f6543 100644 --- a/roles/docker/tasks/install.yml +++ b/roles/docker/tasks/install.yml @@ -32,6 +32,7 @@ content: | deb [arch={{ __docker_apt_arch }} signed-by={{ docker_keyring_path }}] {{ docker_repo_base_url }} {{ ansible_distribution_release }} stable mode: "0644" + register: __docker_apt_source tags: [docker, install] - name: Install Docker Engine packages @@ -40,7 +41,10 @@ name: "{{ docker_packages }}" state: present update_cache: true + register: __docker_packages notify: Restart docker + # Under --check a new source line is only simulated, so apt would find no Docker package. + when: not ansible_check_mode or __docker_apt_source is not changed tags: [docker, install] - name: Enable and start Docker service @@ -49,4 +53,6 @@ name: "{{ docker_service }}" enabled: true state: started + # Under --check on a host without the packages there is no unit to start yet. + when: not ansible_check_mode or (__docker_apt_source is not changed and __docker_packages is not changed) tags: [docker, install] diff --git a/roles/docker/tasks/main.yml b/roles/docker/tasks/main.yml index 96a9491..503c669 100644 --- a/roles/docker/tasks/main.yml +++ b/roles/docker/tasks/main.yml @@ -24,6 +24,8 @@ when: - not (docker_remove | default(false) | bool) - docker_update | bool + # Under --check a new source line is only simulated, so no package would have a candidate. + - not ansible_check_mode or __docker_apt_source | default({}) is not changed tags: [docker, update] - name: Add configured users to the docker group @@ -33,7 +35,10 @@ groups: "{{ docker_group }}" append: true loop: "{{ docker_users }}" - when: not (docker_remove | default(false) | bool) + when: + - not (docker_remove | default(false) | bool) + # Under --check on a host without the packages the group does not exist yet. + - not ansible_check_mode or (__docker_apt_source | default({}) is not changed and __docker_packages | default({}) is not changed) tags: [docker, configure] - name: Install or remove the boot and shutdown units diff --git a/roles/fail2ban/README.md b/roles/fail2ban/README.md index 5588729..35d658c 100644 --- a/roles/fail2ban/README.md +++ b/roles/fail2ban/README.md @@ -82,7 +82,7 @@ Main entry point: `fail2ban` (everything), `fail2ban_install`, `fail2ban_validat ## Tests -The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). The sequence runs prepare, converge, idempotence, side effect and verify; it has no check-mode step. +The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It runs `check` before the converge (a fresh host, where the image already has fail2ban installed) and after it (a configured host). - Converge applies the main entry point with an extra `fail2ban_ignoreip` network and calls `service_jail` twice, one jail with a `logpath` and one with a `journalmatch`. - Converge also runs inputs that must be refused: a service filter without `failregex`, and a `fail2ban_ignoreip` entry with whitespace, a plain string instead of a list, and an entry starting with `;`. The `fail2ban_ignoreip` refusals are matched on their message. @@ -94,6 +94,7 @@ Not covered: no failure is sent through a jail, so a ban triggered by `failregex ## Notes - Idempotent: yes. +- Check mode: `--check` previews a host where fail2ban is already installed; a play-level `check_mode: true` is not supported. On a host without fail2ban it fails at the configuration and service tasks: the package is not installed under `--check`, so `/etc/fail2ban`, `fail2ban-client` and the unit are missing. - `fail2ban_ignoreip` lives in the `[DEFAULT]` section of `jail.local`, so it also applies to the jails that `service_jail` writes to `jail.d`. The exemption only exists once the main entry point has written `jail.local` on that host: `service_jail` does not read `fail2ban_ignoreip`, so a host that uses only `service_jail` has none. - `fail2ban_ignoreip` replaces an `ignoreip` set in `jail.conf` or `jail.d/*.conf`, which Fail2Ban reads before `jail.local`; a `jail.d/*.local` file is read after it and can still override it for one jail. Entries must not contain whitespace, `;` or `%` (a `;` after whitespace starts a comment in Fail2Ban's config files and a `%` is interpolation syntax). - Prefer IP addresses and CIDR networks in `fail2ban_ignoreip`. Fail2Ban resolves a hostname when the jail starts, and a failed lookup leaves that entry ineffective with only a log warning. The validation of `fail2ban_ignoreip` runs under the `fail2ban_validate` tag, so `--tags fail2ban_configure` skips it. diff --git a/roles/fail2ban/molecule/default/molecule.yml b/roles/fail2ban/molecule/default/molecule.yml index b161e35..206269b 100644 --- a/roles/fail2ban/molecule/default/molecule.yml +++ b/roles/fail2ban/molecule/default/molecule.yml @@ -47,13 +47,16 @@ verifier: scenario: # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. + # A check runs before the converge (fresh host) and after it (configured host). test_sequence: - dependency - cleanup - syntax - create - prepare + - check - converge + - check - idempotence - side_effect - verify diff --git a/roles/gitea/README.md b/roles/gitea/README.md index 6aee0c8..5f3fc92 100644 --- a/roles/gitea/README.md +++ b/roles/gitea/README.md @@ -148,7 +148,7 @@ The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_I - The signature check of the download, against local files and throwaway keys: a signature by the pinned key places the binary; a signature by another key, a tampered binary and a key file without the pinned key each fail at the expected task; no download directory is left behind. - The remove path, against a stub unit and binary: both are deleted, the service is no longer active, only the Gitea ufw rules are removed (the SSH rule and a rule without an owner tag stay), and no config or data directory or `gitea_user` account is created. -Not covered: install, configure, the admin user, the SSH-key API calls, the update path, the normal-flow assert on `gitea_api_token` (after the service starts) and a mint that creates a token. +Not covered: install, configure, the admin user, the SSH-key API calls, the update path, the normal-flow assert on `gitea_api_token` (after the service starts) and a mint that creates a token. The scenario runs `check` before and after the converge, but its converge applies nothing, so neither check runs the role. ## Notes @@ -162,3 +162,4 @@ Not covered: install, configure, the admin user, the SSH-key API calls, the upda - The SSH-key API calls go to `gitea_http_addr:gitea_http_port` (wildcard becomes loopback, IPv6 is bracketed), not to `gitea_domain`. - Handlers: `Reload Gitea` restarts the service when `app.ini` changes, and `main.yml` flushes handlers at the end so a later failure in the same play does not drop the restart. The `Reload Systemd` handler is defined but no task in the role notifies it. - The play fails when the service is not `active` after start. +- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. Under `--check` on a host without the binary, the binary assert and the bash completion tasks are skipped. When the systemd unit is new or would change, the start, the running check, the `Reload Gitea` handler and the update swap are skipped. The check-mode path is not tested. diff --git a/roles/gitea/handlers/main.yml b/roles/gitea/handlers/main.yml index e84da7a..6aaa6b6 100644 --- a/roles/gitea/handlers/main.yml +++ b/roles/gitea/handlers/main.yml @@ -9,3 +9,5 @@ ansible.builtin.systemd: name: gitea state: restarted + # Under --check a new unit is only simulated, so systemd would not find it. + when: not ansible_check_mode or __gitea_unit_file | default({}) is not changed diff --git a/roles/gitea/molecule/default/molecule.yml b/roles/gitea/molecule/default/molecule.yml index de433d5..658e893 100644 --- a/roles/gitea/molecule/default/molecule.yml +++ b/roles/gitea/molecule/default/molecule.yml @@ -57,13 +57,16 @@ verifier: scenario: # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. + # A check runs before the converge (fresh host) and after it (configured host). test_sequence: - dependency - cleanup - syntax - create - prepare + - check - converge + - check - idempotence - side_effect - verify diff --git a/roles/gitea/tasks/configure.yml b/roles/gitea/tasks/configure.yml index ef2ca93..5a8a76b 100644 --- a/roles/gitea/tasks/configure.yml +++ b/roles/gitea/tasks/configure.yml @@ -92,6 +92,8 @@ changed_when: false # Read-only: the script is only generated to be written by the next task, so it must also run in --check. check_mode: false + # Under --check on a fresh host there is no binary to run. + when: not ansible_check_mode or gitea_installed tags: [gitea, configure] - name: Write bash completion to system directory @@ -102,6 +104,7 @@ owner: root group: root mode: "0644" + when: not ansible_check_mode or gitea_installed tags: [gitea, configure] - name: Create systemd service file for Gitea @@ -127,6 +130,7 @@ owner: root group: root mode: "0644" + register: __gitea_unit_file tags: [gitea, configure] - name: Reload systemd daemon @@ -164,6 +168,8 @@ name: gitea enabled: true state: started + # Under --check a new unit is only simulated, so systemd would not find it. + when: not ansible_check_mode or __gitea_unit_file is not changed tags: [gitea, configure] - name: Check Gitea service status @@ -176,7 +182,10 @@ - name: Fail if Gitea service is not running ansible.builtin.fail: msg: "Gitea service is not running" - when: gitea_service_status.status.ActiveState != "active" + when: + - gitea_service_status.status.ActiveState != "active" + # The start above is skipped under --check for a new unit. + - not ansible_check_mode or __gitea_unit_file | default({}) is not changed tags: [gitea, configure, health] # The token is minted by the opt-in mint entry point, never here. This sits after the diff --git a/roles/gitea/tasks/install.yml b/roles/gitea/tasks/install.yml index 8467d40..9eb8909 100644 --- a/roles/gitea/tasks/install.yml +++ b/roles/gitea/tasks/install.yml @@ -39,4 +39,6 @@ that: - gitea_binary.stat.exists fail_msg: "Gitea binary not found after download" + # Nothing is downloaded under --check. + when: not ansible_check_mode tags: [gitea, install, health, update] diff --git a/roles/gitea/tasks/update.yml b/roles/gitea/tasks/update.yml index cda2d2c..b856f10 100644 --- a/roles/gitea/tasks/update.yml +++ b/roles/gitea/tasks/update.yml @@ -64,7 +64,8 @@ Gitea was not stopped and still runs {{ gitea_installed_version }}. tags: [gitea, update] - # An include does not pass its tags on to the included tasks, hence apply. + # An include does not pass its tags on to the included tasks, hence apply. Under --check a + # new unit is only simulated, so the stop and start of the swap would not find it. - name: Swap in the staged Gitea binary ansible.builtin.include_role: name: mortis.kamino._binary_update @@ -76,6 +77,7 @@ _binary_update_staged_path: "{{ gitea_staging_dir }}/gitea" _binary_update_version_command: ["{{ gitea_install_dir }}/gitea", --version] _binary_update_expected_version: "{{ gitea_version }}" + when: not ansible_check_mode or __gitea_unit_file | default({}) is not changed tags: [gitea, update] always: diff --git a/roles/gitea_runner/README.md b/roles/gitea_runner/README.md index b39fa1b..6396e88 100644 --- a/roles/gitea_runner/README.md +++ b/roles/gitea_runner/README.md @@ -89,7 +89,7 @@ Change the registration scope of a registered runner: set `gitea_runner_registra ## Tests -The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It runs without a Gitea server and without Docker: a stub `docker.service`, a stub runner binary and a seeded `.runner` stand in for them, and the downloads come from local `file://` URLs. It covers the converge with the prune timer and an update through `_binary_update` (and its idempotence), the rendered `config.yaml` and unit, the prune gate script, the count prune and the disk check against a stub `docker` CLI, turning the pressure prune off, a failed checksum that leaves the runner running, the input asserts, the CI refusal of a forced re-registration and the mint-before-drop order, and the remove path. Registration, the Docker install and the host packages of the host-executor label are not covered. +The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It runs without a Gitea server and without Docker: a stub `docker.service`, a stub runner binary and a seeded `.runner` stand in for them, and the downloads come from local `file://` URLs. It covers the converge with the prune timer and an update through `_binary_update` (and its idempotence), the rendered `config.yaml` and unit, the prune gate script, the count prune and the disk check against a stub `docker` CLI, turning the pressure prune off, a failed checksum that leaves the runner running, the input asserts, the CI refusal of a forced re-registration and the mint-before-drop order, and the remove path. Registration, the Docker install and the host packages of the host-executor label are not covered. It runs `check` before the converge (a fresh host) and after it (a configured host). ## Notes @@ -101,4 +101,5 @@ The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_I - The Docker prune units (`gitea-runner-docker-prune.{service,timer}`) are written only while `gitea_runner_docker_prune_enabled` is true. Setting it back to false leaves them in place: run `systemctl disable --now gitea-runner-docker-prune.timer` by hand. `gitea_runner_remove` deletes them, the pressure units and the prune scripts. A run skipped because a job was running is not retried until the next timer elapse: shorten `gitea_runner_docker_prune_schedule` (e.g. `*-*-* 04:30:00`) if that happens. - The pressure prune (`gitea-runner-docker-prune-pressure.{service,timer}`) runs once per check while usage is above the threshold; it has no low watermark, so usage that stays high needs a smaller count or a larger disk. A threshold set back to `0` stops its timer and removes its units and the disk check script. The age prunes and the count prune both apply: an image either one removes is gone. - The play fails when `gitea-runner` is not `active` after start. +- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. Under `--check`, the work directory ownership task is skipped when the runner group or user is new, and starting Docker when `docker.io` is not installed yet. When the systemd unit is new or would change, the start, the running check, the `Restart Gitea Runner` handler and the update swap are skipped. - `no_log` covers the token mint, the token fact and the registration command. diff --git a/roles/gitea_runner/handlers/main.yml b/roles/gitea_runner/handlers/main.yml index 0d5033e..93be73b 100644 --- a/roles/gitea_runner/handlers/main.yml +++ b/roles/gitea_runner/handlers/main.yml @@ -4,3 +4,5 @@ ansible.builtin.systemd: name: gitea-runner state: restarted + # Under --check a new unit is only simulated, so systemd would not find it. + when: not ansible_check_mode or __gitea_runner_unit_file | default({}) is not changed diff --git a/roles/gitea_runner/molecule/default/molecule.yml b/roles/gitea_runner/molecule/default/molecule.yml index 6f6cc3f..9f10ce2 100644 --- a/roles/gitea_runner/molecule/default/molecule.yml +++ b/roles/gitea_runner/molecule/default/molecule.yml @@ -66,13 +66,16 @@ verifier: scenario: # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. + # A check runs before the converge (fresh host) and after it (configured host). test_sequence: - dependency - cleanup - syntax - create - prepare + - check - converge + - check - idempotence - side_effect - verify diff --git a/roles/gitea_runner/tasks/configure.yml b/roles/gitea_runner/tasks/configure.yml index 2f4d012..cf16334 100644 --- a/roles/gitea_runner/tasks/configure.yml +++ b/roles/gitea_runner/tasks/configure.yml @@ -17,6 +17,10 @@ owner: "{{ gitea_runner_user }}" group: "{{ gitea_runner_group }}" recurse: true + # Under --check a new account is only simulated, so an existing directory cannot be chowned to it. + when: >- + not ansible_check_mode or + (__gitea_runner_group_account | default({}) is not changed and __gitea_runner_user_account | default({}) is not changed) tags: [gitea_runner, configure] - name: Check current runner registration file state @@ -62,6 +66,7 @@ [Install] WantedBy=multi-user.target + register: __gitea_runner_unit_file tags: [gitea_runner, configure] - name: Reload systemd @@ -76,6 +81,8 @@ name: gitea-runner enabled: true state: started + # Under --check a new unit is only simulated, so systemd would not find it. + when: not ansible_check_mode or __gitea_runner_unit_file is not changed tags: [gitea_runner, configure] - name: Check runner service status @@ -88,5 +95,8 @@ - name: Fail if runner service is not running ansible.builtin.fail: msg: "Gitea runner service is not running" - when: gitea_runner_service_status.status.ActiveState != "active" + when: + - gitea_runner_service_status.status.ActiveState != "active" + # The start above is skipped under --check for a new unit. + - not ansible_check_mode or __gitea_runner_unit_file | default({}) is not changed tags: [gitea_runner, configure, health] diff --git a/roles/gitea_runner/tasks/docker.yml b/roles/gitea_runner/tasks/docker.yml index 7b879fe..ba07504 100644 --- a/roles/gitea_runner/tasks/docker.yml +++ b/roles/gitea_runner/tasks/docker.yml @@ -17,6 +17,7 @@ name: - docker.io state: present + register: __gitea_runner_docker_package tags: [gitea_runner, docker, install] - name: Enable and start Docker @@ -25,6 +26,8 @@ name: docker enabled: true state: started + # Under --check a new package is only simulated, so there is no docker unit yet. + when: not ansible_check_mode or __gitea_runner_docker_package is not changed tags: [gitea_runner, docker, install] - name: Add the runner user to the docker group diff --git a/roles/gitea_runner/tasks/prereqs.yml b/roles/gitea_runner/tasks/prereqs.yml index 61e9f21..03e0bda 100644 --- a/roles/gitea_runner/tasks/prereqs.yml +++ b/roles/gitea_runner/tasks/prereqs.yml @@ -6,6 +6,7 @@ ansible.builtin.group: name: "{{ gitea_runner_group }}" system: true + register: __gitea_runner_group_account tags: [gitea_runner, prereqs, always] - name: Ensure runner user exists @@ -16,6 +17,7 @@ home: "{{ gitea_runner_home }}" create_home: true system: true + register: __gitea_runner_user_account tags: [gitea_runner, prereqs, always] - name: Check if runner is installed diff --git a/roles/gitea_runner/tasks/update.yml b/roles/gitea_runner/tasks/update.yml index b76a15b..cbeb262 100644 --- a/roles/gitea_runner/tasks/update.yml +++ b/roles/gitea_runner/tasks/update.yml @@ -54,7 +54,8 @@ The runner was not stopped and still runs {{ gitea_runner_installed_version }}. tags: [gitea_runner, update] - # An include does not pass its tags on to the included tasks, hence apply. + # An include does not pass its tags on to the included tasks, hence apply. Under --check a + # new unit is only simulated, so the stop and start of the swap would not find it. - name: Swap in the staged runner binary ansible.builtin.include_role: name: mortis.kamino._binary_update @@ -66,6 +67,7 @@ _binary_update_staged_path: "{{ gitea_runner_staged_binary }}" _binary_update_version_command: ["{{ gitea_runner_install_dir }}/gitea-runner", --version] _binary_update_expected_version: "{{ gitea_runner_version }}" + when: not ansible_check_mode or __gitea_runner_unit_file | default({}) is not changed tags: [gitea_runner, update] always: diff --git a/roles/network_interfaces/README.md b/roles/network_interfaces/README.md index 4d1fb38..dbb44f0 100644 --- a/roles/network_interfaces/README.md +++ b/roles/network_interfaces/README.md @@ -57,7 +57,7 @@ None. The role does not tag its tasks, so only a play-level tag applies. ## Tests -`molecule/default` runs in CI only, on one platform: Ubuntu 24.04 (noble), the `molecule-ubuntu2404` image (`${MOLECULE_IMAGE}`). The image has no `ifupdown2`, and a real reload could cut the container's own network, so the validate command is `/bin/true` and the apply command appends to a marker file; the rest of the role runs unchanged. Prepare seeds `/etc/network/interfaces` with a legacy body, so the converge has a change to make and a backup to take. +`molecule/default` runs in CI only, on one platform: Ubuntu 24.04 (noble), the `molecule-ubuntu2404` image (`${MOLECULE_IMAGE}`). The image has no `ifupdown2`, and a real reload could cut the container's own network, so the validate command is `/bin/true` and the apply command appends to a marker file; the rest of the role runs unchanged. Prepare seeds `/etc/network/interfaces` with a legacy body, so the converge has a change to make and a backup to take. The scenario runs `check` before the converge (fresh host) and after it (configured host). Verify checks: @@ -76,4 +76,5 @@ It does not test `ifreload`, so no Debian release or `ifupdown2` version is veri - On change the order is: copy the current file to `network_interfaces_backup`, write the new file (notifies `Reload Interfaces`), run the validate command, flush handlers. Any failure in that block restores the backup and fails with "Invalid network configuration detected. Previous config restored." The restore does not reload the interfaces. - The validate command runs after the file is written, and the reload runs right after it. A configuration that passes validation but breaks connectivity is not detected. - With `method: static`, `address` and `netmask` are required by the template. +- Check mode: under `--check` the new file is not written, so the validate command and the reload are skipped; the preview shows whether the file would change, not whether it is valid. - `no_log` is not used: the role handles no secrets. diff --git a/roles/network_interfaces/molecule/default/molecule.yml b/roles/network_interfaces/molecule/default/molecule.yml index fbc51ff..03e3b25 100644 --- a/roles/network_interfaces/molecule/default/molecule.yml +++ b/roles/network_interfaces/molecule/default/molecule.yml @@ -29,13 +29,16 @@ verifier: scenario: # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. + # A check runs before the converge (fresh host) and after it (configured host). test_sequence: - dependency - cleanup - syntax - create - prepare + - check - converge + - check - idempotence - side_effect - verify diff --git a/roles/pihole/README.md b/roles/pihole/README.md index 0edbdc8..5eb2f6d 100644 --- a/roles/pihole/README.md +++ b/roles/pihole/README.md @@ -58,7 +58,7 @@ The input asserts carry `always` (and `assert`), so they run whatever tags are s ## Tests -The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It replaces the upstream repository with a local stub whose installer only logs its arguments and creates the database file, so it tests the merge, the asserts, the user, the configuration directory and the rendered `pihole.toml` (owner, group and mode), the clone's checked-out version, the installer sequence (install once, then reconfigure from the handler) and idempotence, not Pi-hole itself: no pihole-FTL service runs, so that the service loaded the configuration is not verified. It runs `check` before the converge (a first-run preview) and after the idempotence run. Negative tests in `verify.yml` cover a host without Ubuntu facts, a missing `pihole_web_password`, an unset interface, empty upstreams, a non-string password and the placeholder password. +The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It replaces the upstream repository with a local stub whose installer only logs its arguments and creates the database file, so it tests the merge, the asserts, the user, the configuration directory and the rendered `pihole.toml` (owner, group and mode), the clone's checked-out version, the installer sequence (install once, then reconfigure from the handler) and idempotence, not Pi-hole itself: no pihole-FTL service runs, so that the service loaded the configuration is not verified. It runs `check` before the converge (a fresh host) and after it (a configured host). Negative tests in `verify.yml` cover a host without Ubuntu facts, a missing `pihole_web_password`, an unset interface, empty upstreams, a non-string password and the placeholder password. ## Notes diff --git a/roles/pihole/molecule/default/molecule.yml b/roles/pihole/molecule/default/molecule.yml index d67ae0f..65300d9 100644 --- a/roles/pihole/molecule/default/molecule.yml +++ b/roles/pihole/molecule/default/molecule.yml @@ -63,7 +63,7 @@ verifier: scenario: # molecule's default sequence minus both destroys (scripts/run_molecule_scenarios.sh removes the # containers as soon as this scenario ends; after a local run, run `molecule destroy` yourself), - # plus two check steps: one before the converge (first-run preview), one after the idempotence run. + # plus a check before the converge (fresh host) and one after it (configured host). test_sequence: - dependency - cleanup @@ -72,8 +72,8 @@ scenario: - prepare - check - converge - - idempotence - check + - idempotence - side_effect - verify - cleanup diff --git a/roles/pve_lxc/README.md b/roles/pve_lxc/README.md index 6993bba..ac152b8 100644 --- a/roles/pve_lxc/README.md +++ b/roles/pve_lxc/README.md @@ -92,7 +92,7 @@ The input validation (`tasks/validate.yml`) carries `always`, so it runs whateve ## Tests -The `default` molecule scenario runs in CI on Ubuntu 24.04 (noble), the `molecule-ubuntu2404` image, which stands in for the Proxmox node. The API lifecycle (detect, create, update, resize, start, stop, delete and the restarts) is not exercised: there is no Proxmox API in the scenario. The converge includes the internal task files `main.yml` runs on the node side, in its order (`validate`, `firewall`, `raw_config`), with `pve_lxc_state: stopped`, under which the role never restarts the container. Prepare seeds a container config with stale `lxc.*` lines, blockinfile markers and a snapshot section, and backup copies of two containers' configs. Verify checks the reconciled config byte for byte (Proxmox's lines kept in order, the desired `lxc.*` lines after them, the snapshot section untouched), the rendered firewall file, and that only this container's backups were removed. It runs `check` before the converge and after the idempotence run. Negative tests cover a state outside the spec, missing API credentials and a missing network prefix through `main`, and raw config given as a string, outside the `lxc.` namespace or with two lines in one item, a malformed firewall rule and an unknown policy through `validate`; a run with `pve_lxc_manage_lifecycle: false` checks the presence facts. No Proxmox service runs, so pmxcfs's handling of the files and `pve-firewall` loading the rules are not verified. +The `default` molecule scenario runs in CI on Ubuntu 24.04 (noble), the `molecule-ubuntu2404` image, which stands in for the Proxmox node. The API lifecycle (detect, create, update, resize, start, stop, delete and the restarts) is not exercised: there is no Proxmox API in the scenario. The converge includes the internal task files `main.yml` runs on the node side, in its order (`validate`, `firewall`, `raw_config`), with `pve_lxc_state: stopped`, under which the role never restarts the container. Prepare seeds a container config with stale `lxc.*` lines, blockinfile markers and a snapshot section, and backup copies of two containers' configs. Verify checks the reconciled config byte for byte (Proxmox's lines kept in order, the desired `lxc.*` lines after them, the snapshot section untouched), the rendered firewall file, and that only this container's backups were removed. It runs `check` before the converge (a fresh host) and after it (a configured host). Negative tests cover a state outside the spec, missing API credentials and a missing network prefix through `main`, and raw config given as a string, outside the `lxc.` namespace or with two lines in one item, a malformed firewall rule and an unknown policy through `validate`; a run with `pve_lxc_manage_lifecycle: false` checks the presence facts. No Proxmox service runs, so pmxcfs's handling of the files and `pve-firewall` loading the rules are not verified. ## Notes diff --git a/roles/pve_lxc/molecule/default/molecule.yml b/roles/pve_lxc/molecule/default/molecule.yml index 2f8484c..de496f1 100644 --- a/roles/pve_lxc/molecule/default/molecule.yml +++ b/roles/pve_lxc/molecule/default/molecule.yml @@ -57,7 +57,7 @@ verifier: scenario: # molecule's default sequence minus both destroys (scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends; after a local run, run `molecule destroy` yourself), - # plus two check steps: one before the converge (first-run preview), one after the idempotence run. + # plus a check before the converge (fresh host) and one after it (configured host). test_sequence: - dependency - cleanup @@ -66,8 +66,8 @@ scenario: - prepare - check - converge - - idempotence - check + - idempotence - side_effect - verify - cleanup diff --git a/roles/sshd/README.md b/roles/sshd/README.md index 65cbb83..7b20144 100644 --- a/roles/sshd/README.md +++ b/roles/sshd/README.md @@ -59,7 +59,7 @@ The role defines none. ## Tests -The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). Both images ship openssh-server. The sequence runs converge, idempotence and verify; it has no check-mode step. +The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). Both images ship openssh-server. It runs `check` before the converge (a fresh host) and after it (a configured host). - Converge applies the `ubuntu_lxc` profile. It then loads the defaults through `tasks_from: validate.yml` without changing the host: every profile (`pve`, `ubuntu_lxc`, `ubuntu_vm`) passes validation and keeps its root login policy and key algorithms, a consumer `sshd_settings` override wins over the profile, and the `pve` guard accepts each value that allows root key login. - Verify first checks the refusals: a run without `sshd_profile`, `PubkeyAuthentication no` (lock-out), a `pve` profile with `PermitRootLogin no` or `forced-commands-only`, a `pve` profile without `rsa-sha2-512`, SHA-1 `ssh-rsa`, and an unknown profile. Each but the missing profile is matched on its message. @@ -71,6 +71,7 @@ Not covered: the `pve` profile is not converged and no SSH login is attempted. ## Notes - Idempotent: yes. +- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. - `tasks/validate.yml` fails the play for an unknown profile, an empty merged result, `PasswordAuthentication no` without `PubkeyAuthentication yes`, a `pve` profile whose `PermitRootLogin` does not allow root key login (`prohibit-password`, its alias `without-password`, or `yes`; `no` and `forced-commands-only` are refused), a `pve` profile whose `HostKeyAlgorithms` or `PubkeyAcceptedAlgorithms` lacks `rsa-sha2-512` (the `+`/`-`/`^` list forms are not parsed and fail; write the full list), and any profile that lists SHA-1 `ssh-rsa` in either. - The drop-in is installed with `validate: sshd -t -f %s`. The handler named `Restart SSH` reloads the service, and is skipped in check mode or when `sshd_reload` is false. - `ssh-keygen -A` runs on every play and always reports ok. diff --git a/roles/sshd/molecule/default/molecule.yml b/roles/sshd/molecule/default/molecule.yml index 2578462..d0c2f6b 100644 --- a/roles/sshd/molecule/default/molecule.yml +++ b/roles/sshd/molecule/default/molecule.yml @@ -47,13 +47,16 @@ verifier: scenario: # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. + # A check runs before the converge (fresh host) and after it (configured host). test_sequence: - dependency - cleanup - syntax - create - prepare + - check - converge + - check - idempotence - side_effect - verify diff --git a/roles/system_baseline/README.md b/roles/system_baseline/README.md index 606e9cd..480678a 100644 --- a/roles/system_baseline/README.md +++ b/roles/system_baseline/README.md @@ -124,7 +124,7 @@ The two task files besides `main` are a supported interface, for a caller that d ## Tests -The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It installs packages from the Ubuntu mirrors, so the runner needs outbound network. The sequence runs prepare, converge, idempotence and verify; it has no check-mode step. +The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It installs packages from the Ubuntu mirrors, so the runner needs outbound network. It runs `check` before the converge (a fresh host) and after it (a configured host). - Converge first checks that the role refuses a bad input with its own message before any change: an unknown `system_baseline_system_type`, an unknown `system_baseline_apt_upgrade` (main flow and `update` entry point), a negative reboot throttle or a negative or boolean post-reboot delay (main flow and `reboot` entry point), a line break in `system_baseline_apt_daily_timeout`, and a bad package blacklist (a quote, a null item, a bare string). - Converge then applies the `ubuntu_lxc` profile, with tmux autostart and `system_baseline_apt_daily_timeout: 1h`, and only resolves the `pve` and `ubuntu_vm` profiles and a consumer override, asserting the resolved inputs. @@ -207,6 +207,8 @@ The upgrade runs over the SSH connection Ansible uses. An `openssh-server` upgra In a dry run (`--check`) the main flow does not run the apt upgrade: a package that is only waiting for the next patch run, or a package cache older than an hour, is not drift, and the package module reports both as `changed`, so a scheduled dry run would flag every host whenever a package landed in between. The autoremove and autoclean step still runs, and is checked: a leftover package it would remove is reported as `changed`, on purpose. `apt-get autoclean` runs in this step in a real run, in the main flow and in the `update` entry point. The `update` entry point is unchanged: it is an explicit request to update, so in check mode it still previews the upgrade. +`--check` is the supported preview; a play-level `check_mode: true` is not supported. + ### Unattended-upgrades origins A profile's `unattended_upgrade_origins` list pins the origins unattended-upgrades may install from. apt merges every file in `/etc/apt/apt.conf.d` and appends list entries across files, so a later file that only lists origins would widen the distribution's `50unattended-upgrades`, never narrow it. A non-empty list is therefore written to `/etc/apt/apt.conf.d/90unattended-upgrades-origins`, which sorts after `50unattended-upgrades`, clears `Unattended-Upgrade::Allowed-Origins` and `Unattended-Upgrade::Origins-Pattern` (apt's `#clear` directive) and sets only the listed patterns. The origin lists in `50unattended-upgrades` itself are not edited. An empty or missing list removes the drop-in. diff --git a/roles/system_baseline/molecule/default/molecule.yml b/roles/system_baseline/molecule/default/molecule.yml index 98eeecb..8746c3b 100644 --- a/roles/system_baseline/molecule/default/molecule.yml +++ b/roles/system_baseline/molecule/default/molecule.yml @@ -53,13 +53,16 @@ verifier: scenario: # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. + # A check runs before the converge (fresh host) and after it (configured host). test_sequence: - dependency - cleanup - syntax - create - prepare + - check - converge + - check - idempotence - side_effect - verify diff --git a/roles/systemd_hardening/README.md b/roles/systemd_hardening/README.md index 8214d26..4aee8be 100644 --- a/roles/systemd_hardening/README.md +++ b/roles/systemd_hardening/README.md @@ -52,7 +52,7 @@ Only `always` (the `systemd_hardening_system_type` assert) works as intended. `d ## Tests -`molecule/default` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It runs the `ubuntu_lxc` profile against stub unit files (a socket and a service for the ones that ship both, a service only and a socket only for the others), because the test images install none of the services the role disables or masks. It checks that the stub sockets and services end up stopped and disabled or masked, that a listed unit that does not exist is skipped, the manager and service drop-ins (loaded by systemd after the reload) and the refusals: a missing or unknown `systemd_hardening_system_type`, and a `systemd_hardening_services` entry or value that is not a mapping. It cannot show that a real `rpcbind` or similar stops. The `pve` and `ubuntu_vm` profiles and Debian 13 are not verified by a test. +`molecule/default` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It runs the `ubuntu_lxc` profile against stub unit files (a socket and a service for the ones that ship both, a service only and a socket only for the others), because the test images install none of the services the role disables or masks. It checks that the stub sockets and services end up stopped and disabled or masked, that a listed unit that does not exist is skipped, the manager and service drop-ins (loaded by systemd after the reload) and the refusals: a missing or unknown `systemd_hardening_system_type`, and a `systemd_hardening_services` entry or value that is not a mapping. It runs `check` before the converge (fresh host) and after it (configured host). It cannot show that a real `rpcbind` or similar stops. The `pve` and `ubuntu_vm` profiles and Debian 13 are not verified by a test. ## Notes diff --git a/roles/systemd_hardening/molecule/default/molecule.yml b/roles/systemd_hardening/molecule/default/molecule.yml index cbddf31..906a943 100644 --- a/roles/systemd_hardening/molecule/default/molecule.yml +++ b/roles/systemd_hardening/molecule/default/molecule.yml @@ -47,13 +47,16 @@ verifier: scenario: # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. + # A check runs before the converge (fresh host) and after it (configured host). test_sequence: - dependency - cleanup - syntax - create - prepare + - check - converge + - check - idempotence - side_effect - verify diff --git a/roles/traefik/README.md b/roles/traefik/README.md index bc23b50..48503a1 100644 --- a/roles/traefik/README.md +++ b/roles/traefik/README.md @@ -79,7 +79,7 @@ Update or remove: pass `-e traefik_update=true`, or `-e "traefik_remove=true tra ## Tests -The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It needs outbound network: the role installs packages with apt and downloads real Traefik releases from GitHub. Converge runs `mortis.kamino.ufw`, then the role. Verify covers: +The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It needs outbound network: the role installs packages with apt and downloads real Traefik releases from GitHub. Converge runs `mortis.kamino.ufw`, then the role. It runs `check` before the converge (on a fresh host) and after it (on the configured host). Verify covers: - The input guards, first: one play expects the failure message containing `unauthenticated Traefik dashboard` (the dashboard guard: enabled without valid `user:hash` users); the next expects `collides with the middleware`, raised by the dashboard-auth collision assert, which runs whenever users are set, also with the dashboard off. - The install: the pinned version runs, and only the binary is left from the archive. @@ -94,6 +94,7 @@ The scenario disables the namespace-based sandboxing and the `User=` switch with ## Notes - Idempotent: yes. +- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. On a host without the Traefik binary, the ACME file permissions, the service start and its running check, the `Restart Traefik` handler and the `traefik_update` tasks are skipped under `--check`, because nothing is extracted and the unit file is only simulated. - The role is not a state selector. `main.yml` runs prerequisites and configure unless `traefik_remove` is true; install runs only when the binary is absent; update only when `traefik_update` is true; remove only when `traefik_remove` is true, and then every other task in `main.yml` is skipped. - Download integrity: the archive SHA-256 must appear in the release's checksum file or the play fails. - Secrets: `traefik.env` (mode 0600, root) and `dynamic.yml` are rendered with `no_log: true`, and no backups are kept, so `--diff` never shows them. `dynamic.yml` may hold dashboard htpasswd hashes. diff --git a/roles/traefik/handlers/main.yml b/roles/traefik/handlers/main.yml index 91c9c65..9e5aa19 100644 --- a/roles/traefik/handlers/main.yml +++ b/roles/traefik/handlers/main.yml @@ -9,3 +9,5 @@ ansible.builtin.systemd: name: traefik state: restarted + # Under --check on a first install there is no unit to restart yet. + when: not ansible_check_mode or traefik_installed | bool diff --git a/roles/traefik/molecule/default/molecule.yml b/roles/traefik/molecule/default/molecule.yml index 8e3b56d..8288582 100644 --- a/roles/traefik/molecule/default/molecule.yml +++ b/roles/traefik/molecule/default/molecule.yml @@ -68,13 +68,16 @@ verifier: scenario: # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. + # A check runs before the converge (fresh host) and after it (configured host). test_sequence: - dependency - cleanup - syntax - create - prepare + - check - converge + - check - idempotence - side_effect - verify diff --git a/roles/traefik/tasks/binary/configure.yml b/roles/traefik/tasks/binary/configure.yml index 3e12e8a..57771d3 100644 --- a/roles/traefik/tasks/binary/configure.yml +++ b/roles/traefik/tasks/binary/configure.yml @@ -196,6 +196,8 @@ name: traefik enabled: true state: started + # Under --check on a first install the unit file is only simulated, so systemd would not find it. + when: not ansible_check_mode or traefik_installed | bool tags: [traefik, configure] - name: Check Traefik service status @@ -208,5 +210,7 @@ - name: Fail if Traefik service is not running ansible.builtin.fail: msg: "Traefik service is not running" - when: traefik_service_status.status.ActiveState != "active" + when: + - not ansible_check_mode or traefik_installed | bool + - traefik_service_status.status.ActiveState != "active" tags: [traefik, configure, health] diff --git a/roles/traefik/tasks/binary/install.yml b/roles/traefik/tasks/binary/install.yml index 84cdc96..65ac7f6 100644 --- a/roles/traefik/tasks/binary/install.yml +++ b/roles/traefik/tasks/binary/install.yml @@ -21,6 +21,7 @@ cmd: touch {{ traefik_acme_file }} args: creates: "{{ traefik_acme_file }}" + register: __traefik_acme_touch tags: [traefik, install, binary, update] - name: Ensure correct permissions on ACME file @@ -30,6 +31,8 @@ owner: "{{ traefik_user }}" group: "{{ traefik_user }}" mode: "0600" + # Under --check the touch above is skipped, and the file module fails on a missing file. + when: not ansible_check_mode or __traefik_acme_touch is not changed tags: [traefik, install, binary, update] - name: Install the Traefik binary @@ -49,4 +52,6 @@ that: - traefik_binary.stat.exists fail_msg: "Traefik binary not found after extraction" + # Nothing is extracted under --check. + when: not ansible_check_mode tags: [traefik, install, binary, health, update] diff --git a/roles/traefik/tasks/main.yml b/roles/traefik/tasks/main.yml index b52573c..7881ef4 100644 --- a/roles/traefik/tasks/main.yml +++ b/roles/traefik/tasks/main.yml @@ -63,4 +63,6 @@ when: - traefik_update | bool - not (traefik_remove | default(false) | bool) + # Under --check on a first install no binary is extracted, so the update would try to swap a service that does not exist. + - not ansible_check_mode or traefik_installed | bool tags: [traefik, update] diff --git a/roles/ufw/README.md b/roles/ufw/README.md index 9c71c05..c70f54c 100644 --- a/roles/ufw/README.md +++ b/roles/ufw/README.md @@ -136,7 +136,7 @@ Skipping `ufw_aggregate` leaves the lists the later tasks read (`ufw_aggregated_ ## Tests -The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). Both containers get `NET_ADMIN`. The sequence runs prepare, converge, idempotence and verify; it has no check-mode step. +The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). Both containers get `NET_ADMIN`. The sequence runs prepare, `check` before the converge (fresh host), converge, `check` after it (configured host), idempotence and verify. - Prepare installs ufw and stores rules the role must handle: an untagged manual rule, a manual rule the base list deletes, and tagged rules of service owners that are stale, removed or orphaned, some of them looking like an SSH path (port 22 with a source, no port, a range, an application profile named like SSH). - Converge applies the base `ufw_rules` (one entry with `delete: true`), then calls `service_rules` for several owners (purge on, purge off, `absent`), and `orphan_rules` with no list, an empty list, report only and purge. @@ -145,7 +145,7 @@ The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_ - Verify checks `before.rules`: the loopback accept, the INVALID-state drop behind its logging rule and in its place among the input rules. - Verify runs `service_rules` under the `check_mode` keyword and checks that the rule list did not change. -Not covered: `ufw_before_rules` entries and `ufw_sysctl_overrides` are never set, and `orphan_rules` is not run in check mode. +Not covered: `ufw_before_rules` entries and `ufw_sysctl_overrides` are never set, and `orphan_rules` runs in check mode only under `--check` (the `check` steps), not under the `check_mode` keyword. ## Notes @@ -156,4 +156,5 @@ Not covered: `ufw_before_rules` entries and `ufw_sysctl_overrides` are never set - Rule schema is asserted before anything changes: `rule` must be allow, deny, reject or limit, `port` a positive integer, `proto` tcp or udp, and each rule needs `port`, `from`, `to` or `interface`. - UFW is only re-enabled when `ufw status verbose` shows it inactive or the logging line differs, because the module rewrites rules and re-applies sysctl on every call. - The `Reload UFW` handler runs `ufw reload`. +- Check mode: `--check` previews a host where ufw is already installed. On a host without ufw it fails at the default policy tasks: the package is not installed under `--check`, and the module needs the `ufw` binary. - `no_log` is not used: the role handles no secrets. diff --git a/roles/ufw/molecule/default/converge.yml b/roles/ufw/molecule/default/converge.yml index 964414e..16a9610 100644 --- a/roles/ufw/molecule/default/converge.yml +++ b/roles/ufw/molecule/default/converge.yml @@ -110,8 +110,8 @@ # svc_o2 that looks like the SSH rule, and a rule of svc_m. The managed owners below are # the ones used above plus svc_m (listed but never called), so svc_o1 and svc_o2 are # orphans. The cases that must change nothing run first and the purge last, so the -# idempotence run finds nothing left to delete. Check mode is not exercised: whether it -# reaches the tasks of an include_role is not verified. +# idempotence run finds nothing left to delete. Under --check the role's own skipped probe +# keeps the purge from deleting, and orphan_check.yml skips its counts. - name: Report and purge the rules of unmanaged owners hosts: all gather_facts: false diff --git a/roles/ufw/molecule/default/molecule.yml b/roles/ufw/molecule/default/molecule.yml index 2cfeef0..c1328d7 100644 --- a/roles/ufw/molecule/default/molecule.yml +++ b/roles/ufw/molecule/default/molecule.yml @@ -47,13 +47,16 @@ verifier: scenario: # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. + # A check runs before the converge (fresh host) and after it (configured host). test_sequence: - dependency - cleanup - syntax - create - prepare + - check - converge + - check - idempotence - side_effect - verify diff --git a/roles/ufw/molecule/default/tasks/orphan_check.yml b/roles/ufw/molecule/default/tasks/orphan_check.yml index d7a618e..4bda572 100644 --- a/roles/ufw/molecule/default/tasks/orphan_check.yml +++ b/roles/ufw/molecule/default/tasks/orphan_check.yml @@ -10,6 +10,7 @@ changed_when: false environment: {LC_ALL: C} +# Under --check (molecule's check step) nothing was applied or purged, so the counts do not hold. - name: The seeds are as this case leaves them ansible.builtin.assert: that: @@ -25,3 +26,4 @@ quiet: true vars: ufw_conv_o1_count: "{{ ufw_conv_o1_want | default(ufw_conv_o1_initial) }}" + when: not ansible_check_mode diff --git a/roles/user_management/README.md b/roles/user_management/README.md index 8bf0c68..dd528a1 100644 --- a/roles/user_management/README.md +++ b/roles/user_management/README.md @@ -64,7 +64,7 @@ Both are set under the `always` tag. ## Tests -`molecule/default` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). Prepare creates a stale user to remove, plants an extra key in an existing user's `authorized_keys` for authoritative mode to drop, and sets root's password before converge; the role then locks it. The converge sets the primary user explicitly instead of through the `{{ ansible_user }}` default. +`molecule/default` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). Prepare creates a stale user to remove, plants an extra key in an existing user's `authorized_keys` for authoritative mode to drop, and sets root's password before converge; the role then locks it. The converge sets the primary user explicitly instead of through the `{{ ansible_user }}` default. It runs `check` before the converge (a fresh host) and after it (a configured host). Verify checks: @@ -79,6 +79,7 @@ The scenario uses the docker connection, where `wait_for_connection` always succ ## Notes - Idempotent: yes. +- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. Under `--check` the `authorized_keys` of a user that does not exist yet are skipped, because the user is not created. - `user_management_remove_home` is applied to every user task, so it also passes `remove: true` for present users, which only matters for `state: absent`. - Per-user `authorized_keys` are added to `user_management_default_authorized_keys`. Keys are written only for users whose `state` is present and whose merged list is non-empty. With `user_management_authoritative` true other keys are removed. - `tasks/ssh_keys.yml` hardcodes the `.ssh` path as `/home//.ssh` and skips creating it for users with `system: true`, but the `authorized_key` task still runs for them (it uses the user's real home), so a system user with keys gets them written. diff --git a/roles/user_management/molecule/default/molecule.yml b/roles/user_management/molecule/default/molecule.yml index eb83b20..25d46dd 100644 --- a/roles/user_management/molecule/default/molecule.yml +++ b/roles/user_management/molecule/default/molecule.yml @@ -55,13 +55,16 @@ verifier: scenario: # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. + # A check runs before the converge (fresh host) and after it (configured host). test_sequence: - dependency - cleanup - syntax - create - prepare + - check - converge + - check - idempotence - side_effect - verify diff --git a/roles/user_management/tasks/ssh_keys.yml b/roles/user_management/tasks/ssh_keys.yml index 5f32af2..37f5d10 100644 --- a/roles/user_management/tasks/ssh_keys.yml +++ b/roles/user_management/tasks/ssh_keys.yml @@ -14,6 +14,12 @@ - item.state | default('present') == 'present' - not item.system | default(false) +# Under --check a user the role would create does not exist yet, and authorized_key fails on a missing user. +- name: Read the existing users for the check-mode preview + ansible.builtin.getent: + database: passwd + when: ansible_check_mode + - name: Enforce authorized_keys authoritatively become: true ansible.posix.authorized_key: @@ -25,5 +31,6 @@ when: - item.state | default('present') == 'present' - ( user_management_default_authorized_keys + (item.authorized_keys | default([])) ) | length > 0 + - not ansible_check_mode or item.name in ansible_facts.getent_passwd loop_control: label: "{{ item.name }}" diff --git a/tests/scripts/test_run_molecule_scenarios.py b/tests/scripts/test_run_molecule_scenarios.py index 967d116..d46ea3e 100644 --- a/tests/scripts/test_run_molecule_scenarios.py +++ b/tests/scripts/test_run_molecule_scenarios.py @@ -395,6 +395,21 @@ class ScenarioPlatformsTest(unittest.TestCase): seen[name] = where self.assertNotIn("destroy", data["scenario"]["test_sequence"], path) + def test_role_scenarios_run_check_before_and_after_converge(self): + # A check-only scenario has no converge to surround, so it is not held to this. + files = sorted(REPO.glob("roles/*/molecule/*/molecule.yml")) + self.assertTrue(files) + for path in files: + sequence = yaml.safe_load(path.read_text())["scenario"]["test_sequence"] + if "converge" not in sequence: + continue + at = sequence.index("converge") + self.assertEqual( + sequence[at - 1 : at + 3], + ["check", "converge", "check", "idempotence"], + path.relative_to(REPO), + ) + def test_the_workflow_finds_the_containers_by_the_run_label(self): # the sweep of earlier runs filters by the key, the cleanup of this run by key and value self.assertEqual( From 3afff00b9ba13d7f9c792bb56a221451921e71e8 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Thu, 26 Nov 2026 11:11:19 +0000 Subject: [PATCH 2/3] fix(roles): apply review findings to the check-mode change Guard gitea_runner's docker group membership like its Docker start, run user_management's getent probe unconditionally, and state the play-level check_mode limit in every touched README. The check-mode rule now allows the pihole guard form and names roles whose --check needs their package installed. Assisted-by: Claude Code --- .gitea/workflows/AGENTS.md | 3 ++- roles/AGENTS.md | 9 ++++++--- roles/gitea_runner/tasks/docker.yml | 2 ++ roles/network_interfaces/README.md | 2 +- roles/systemd_hardening/README.md | 1 + roles/ufw/README.md | 2 +- roles/user_management/tasks/ssh_keys.yml | 1 - tests/scripts/test_run_molecule_scenarios.py | 4 ++++ 8 files changed, 17 insertions(+), 7 deletions(-) diff --git a/.gitea/workflows/AGENTS.md b/.gitea/workflows/AGENTS.md index 38c3a8a..f5afa9c 100644 --- a/.gitea/workflows/AGENTS.md +++ b/.gitea/workflows/AGENTS.md @@ -110,7 +110,8 @@ issue. *Why:* a pull request that raises the ansible-core floor must run its scenarios on the new core before it automerges. *Held:* control: the pin comparison in `molecule.yml`. - **Every scenario sets `scenario.test_sequence` to molecule's default minus both `destroy` - steps, with a role scenario's `check` right before and right after `converge`, names each platform `${KAMINO_RUN_ID:-local}-` and labels it + steps, with a role scenario's `check` right before and right after `converge`, names each + platform `${KAMINO_RUN_ID:-local}-` and labels it `net.rkdr.mortis.molecule-run: "${KAMINO_RUN_ID:-local}"` in its `molecule.yml`;** copy it into a new scenario. *Why:* the run script removes each scenario's containers by those names as soon as it ends, which skips the driver's destroy wait, the workflow finds a run's containers by the diff --git a/roles/AGENTS.md b/roles/AGENTS.md index a836163..ebb3e65 100644 --- a/roles/AGENTS.md +++ b/roles/AGENTS.md @@ -96,9 +96,12 @@ names a check that does not exist yet and its backlog issue. block/rescue/assert matching the expected error message, run last or failing before any change. *Why:* a bare "it failed" passes for the wrong error. *Held:* review. - **Check mode.** Every scenario runs `check` before and after converge. The only guards are - `check_mode: false` on read-only probes and `when: not ansible_check_mode`; `--check` is the - supported preview, and the README says a play-level `check_mode: true` is not supported. A role - that cannot support check mode says why in README Notes and drops the step. *Why:* check-mode + `check_mode: false` on read-only probes and `when: not ansible_check_mode`, alone or as + `not ansible_check_mode or ` where the condition shows the task can run, such as a + registered result that is not changed; `--check` is the supported preview, and the README says + a play-level `check_mode: true` is not supported. A role whose `--check` needs its package + installed says so in README Notes and keeps the step; a role that cannot support check mode at + all says why there and drops the step, with an exception in the test below. *Why:* check-mode breakage is a frequent fix, and one guard pattern is reviewable. *Held:* Molecule for the run passing, `tests/scripts/test_run_molecule_scenarios.py` for the steps being there; review for the guards. diff --git a/roles/gitea_runner/tasks/docker.yml b/roles/gitea_runner/tasks/docker.yml index ba07504..e849829 100644 --- a/roles/gitea_runner/tasks/docker.yml +++ b/roles/gitea_runner/tasks/docker.yml @@ -36,6 +36,8 @@ name: "{{ gitea_runner_user }}" groups: docker append: true + # Under --check a new docker.io creates no docker group, and user fails on a missing group. + when: not ansible_check_mode or __gitea_runner_docker_package is not changed tags: [gitea_runner, docker, install] # Pulls ubuntu:24.04 from Docker Hub. Also proves Docker runs inside a container host diff --git a/roles/network_interfaces/README.md b/roles/network_interfaces/README.md index dbb44f0..7901ac2 100644 --- a/roles/network_interfaces/README.md +++ b/roles/network_interfaces/README.md @@ -76,5 +76,5 @@ It does not test `ifreload`, so no Debian release or `ifupdown2` version is veri - On change the order is: copy the current file to `network_interfaces_backup`, write the new file (notifies `Reload Interfaces`), run the validate command, flush handlers. Any failure in that block restores the backup and fails with "Invalid network configuration detected. Previous config restored." The restore does not reload the interfaces. - The validate command runs after the file is written, and the reload runs right after it. A configuration that passes validation but breaks connectivity is not detected. - With `method: static`, `address` and `netmask` are required by the template. -- Check mode: under `--check` the new file is not written, so the validate command and the reload are skipped; the preview shows whether the file would change, not whether it is valid. +- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. Under `--check` the new file is not written, so the validate command and the reload are skipped; the preview shows whether the file would change, not whether it is valid. - `no_log` is not used: the role handles no secrets. diff --git a/roles/systemd_hardening/README.md b/roles/systemd_hardening/README.md index 4aee8be..2e5c997 100644 --- a/roles/systemd_hardening/README.md +++ b/roles/systemd_hardening/README.md @@ -57,6 +57,7 @@ Only `always` (the `systemd_hardening_system_type` assert) works as intended. `d ## Notes - Idempotent: yes. +- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. - Every name in `systemd_hardening_disable_services` and `systemd_hardening_mask_services` is looked up as a socket and as a service, with or without the `.service` or `.socket` suffix. Only units that systemd has a unit file for (`LoadState` is not `not-found`) are disabled or masked, the socket first: a stopped service whose socket still listens is started again by the next connection. A name with only a service, only a socket, or neither is fine. The role asks about the listed units only, with one `systemctl show`, and does not list every unit file. A template name such as `foo@.service` is not supported: that call fails. - `systemctl disable` does nothing for a static unit (one with no `[Install]` section), so the systemd module can report `changed` on every run for it; list only names whose units can be enabled, and note that this now applies to a listed name's socket as well. - The manager drop-in (`DefaultLimitNOFILE`, `DefaultLimitNPROC`, `DefaultLimitCORE`, and task, memory and CPU accounting) is written verbatim by `tasks/harden_manager.yml` and has no variable. diff --git a/roles/ufw/README.md b/roles/ufw/README.md index c70f54c..811f928 100644 --- a/roles/ufw/README.md +++ b/roles/ufw/README.md @@ -156,5 +156,5 @@ Not covered: `ufw_before_rules` entries and `ufw_sysctl_overrides` are never set - Rule schema is asserted before anything changes: `rule` must be allow, deny, reject or limit, `port` a positive integer, `proto` tcp or udp, and each rule needs `port`, `from`, `to` or `interface`. - UFW is only re-enabled when `ufw status verbose` shows it inactive or the logging line differs, because the module rewrites rules and re-applies sysctl on every call. - The `Reload UFW` handler runs `ufw reload`. -- Check mode: `--check` previews a host where ufw is already installed. On a host without ufw it fails at the default policy tasks: the package is not installed under `--check`, and the module needs the `ufw` binary. +- Check mode: `--check` previews a host where ufw is already installed; a play-level `check_mode: true` is not supported. On a host without ufw it fails at the default policy tasks: the package is not installed under `--check`, and the module needs the `ufw` binary. - `no_log` is not used: the role handles no secrets. diff --git a/roles/user_management/tasks/ssh_keys.yml b/roles/user_management/tasks/ssh_keys.yml index 37f5d10..8ba75be 100644 --- a/roles/user_management/tasks/ssh_keys.yml +++ b/roles/user_management/tasks/ssh_keys.yml @@ -18,7 +18,6 @@ - name: Read the existing users for the check-mode preview ansible.builtin.getent: database: passwd - when: ansible_check_mode - name: Enforce authorized_keys authoritatively become: true diff --git a/tests/scripts/test_run_molecule_scenarios.py b/tests/scripts/test_run_molecule_scenarios.py index d46ea3e..1804ed0 100644 --- a/tests/scripts/test_run_molecule_scenarios.py +++ b/tests/scripts/test_run_molecule_scenarios.py @@ -395,6 +395,10 @@ class ScenarioPlatformsTest(unittest.TestCase): seen[name] = where self.assertNotIn("destroy", data["scenario"]["test_sequence"], path) + +class ScenarioSequenceTest(unittest.TestCase): + """A role scenario previews the fresh host and the configured host with molecule's check step.""" + def test_role_scenarios_run_check_before_and_after_converge(self): # A check-only scenario has no converge to surround, so it is not held to this. files = sorted(REPO.glob("roles/*/molecule/*/molecule.yml")) From 9ce0fbd4332fc5fa353d096d65f7a4ec13cdaebb Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Thu, 26 Nov 2026 12:09:15 +0000 Subject: [PATCH 3/3] fix(roles): refresh apt in the docker and traefik scenarios and apply review round 2 The molecule images ship without apt lists, and --check does not refresh them, so the fresh-host check of docker and traefik could not find curl and wget. Their prepare now refreshes the cache, as pihole's and system_baseline's already do by installing fixtures. The sequence test gets its own class after the platform tests, the check-mode rule is split into guards and README, and the user_management comment sits on the task it explains. Assisted-by: Claude Code --- roles/AGENTS.md | 20 +++++------ roles/docker/molecule/default/prepare.yml | 6 ++++ roles/traefik/molecule/default/prepare.yml | 6 ++++ roles/user_management/tasks/ssh_keys.yml | 2 +- tests/scripts/test_run_molecule_scenarios.py | 38 ++++++++++---------- 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/roles/AGENTS.md b/roles/AGENTS.md index ebb3e65..9f63791 100644 --- a/roles/AGENTS.md +++ b/roles/AGENTS.md @@ -95,16 +95,16 @@ names a check that does not exist yet and its backlog issue. - **Negative tests.** Every validation has one in `verify.yml` (or a file it includes): block/rescue/assert matching the expected error message, run last or failing before any change. *Why:* a bare "it failed" passes for the wrong error. *Held:* review. -- **Check mode.** Every scenario runs `check` before and after converge. The only guards are - `check_mode: false` on read-only probes and `when: not ansible_check_mode`, alone or as - `not ansible_check_mode or ` where the condition shows the task can run, such as a - registered result that is not changed; `--check` is the supported preview, and the README says - a play-level `check_mode: true` is not supported. A role whose `--check` needs its package - installed says so in README Notes and keeps the step; a role that cannot support check mode at - all says why there and drops the step, with an exception in the test below. *Why:* check-mode - breakage is a frequent fix, and one guard pattern is reviewable. *Held:* Molecule for the run - passing, `tests/scripts/test_run_molecule_scenarios.py` for the steps being there; review for - the guards. +- **Check mode.** Every scenario runs `check` before and after converge. Guards: + `check_mode: false` on read-only probes, and `when: not ansible_check_mode`, alone or as + `not ansible_check_mode or ` where the condition shows the task can run (such as a + registered result that is not changed). README: `--check` is the supported preview and a + play-level `check_mode: true` is not supported; a role whose `--check` needs its package + installed says so in Notes and keeps the step; a role that cannot support check mode at all + says why in Notes and drops the step, which also needs an exception in the test named below. + *Why:* check-mode breakage is a frequent fix, and one guard pattern is reviewable. *Held:* + Molecule for the run passing, `tests/scripts/test_run_molecule_scenarios.py` for the steps + being there; review for the guards. ## Roles calling roles diff --git a/roles/docker/molecule/default/prepare.yml b/roles/docker/molecule/default/prepare.yml index c79cae1..32dbd94 100644 --- a/roles/docker/molecule/default/prepare.yml +++ b/roles/docker/molecule/default/prepare.yml @@ -12,6 +12,12 @@ docker_molecule_repo: /srv/docker-stub-repo docker_molecule_work: /var/tmp/docker-stub-work tasks: + # The image ships without apt lists, and --check does not refresh them, so the fresh-host + # check finds the packages the role installs. + - name: Refresh the apt cache + ansible.builtin.apt: + update_cache: true + - name: Copy the repository builder ansible.builtin.copy: src: build-stub-repo.sh diff --git a/roles/traefik/molecule/default/prepare.yml b/roles/traefik/molecule/default/prepare.yml index e81eda7..5c82815 100644 --- a/roles/traefik/molecule/default/prepare.yml +++ b/roles/traefik/molecule/default/prepare.yml @@ -14,6 +14,12 @@ hosts: all gather_facts: false tasks: + # The image ships without apt lists, and --check does not refresh them, so the fresh-host + # check finds the packages the role installs. + - name: Refresh the apt cache + ansible.builtin.apt: + update_cache: true + - name: Create the unit drop-in directory ansible.builtin.file: path: /etc/systemd/system/traefik.service.d diff --git a/roles/user_management/tasks/ssh_keys.yml b/roles/user_management/tasks/ssh_keys.yml index 8ba75be..fb2ce51 100644 --- a/roles/user_management/tasks/ssh_keys.yml +++ b/roles/user_management/tasks/ssh_keys.yml @@ -14,11 +14,11 @@ - item.state | default('present') == 'present' - not item.system | default(false) -# Under --check a user the role would create does not exist yet, and authorized_key fails on a missing user. - name: Read the existing users for the check-mode preview ansible.builtin.getent: database: passwd +# Under --check a user the role would create does not exist yet, and authorized_key fails on a missing user. - name: Enforce authorized_keys authoritatively become: true ansible.posix.authorized_key: diff --git a/tests/scripts/test_run_molecule_scenarios.py b/tests/scripts/test_run_molecule_scenarios.py index 1804ed0..ddee564 100644 --- a/tests/scripts/test_run_molecule_scenarios.py +++ b/tests/scripts/test_run_molecule_scenarios.py @@ -395,25 +395,6 @@ class ScenarioPlatformsTest(unittest.TestCase): seen[name] = where self.assertNotIn("destroy", data["scenario"]["test_sequence"], path) - -class ScenarioSequenceTest(unittest.TestCase): - """A role scenario previews the fresh host and the configured host with molecule's check step.""" - - def test_role_scenarios_run_check_before_and_after_converge(self): - # A check-only scenario has no converge to surround, so it is not held to this. - files = sorted(REPO.glob("roles/*/molecule/*/molecule.yml")) - self.assertTrue(files) - for path in files: - sequence = yaml.safe_load(path.read_text())["scenario"]["test_sequence"] - if "converge" not in sequence: - continue - at = sequence.index("converge") - self.assertEqual( - sequence[at - 1 : at + 3], - ["check", "converge", "check", "idempotence"], - path.relative_to(REPO), - ) - def test_the_workflow_finds_the_containers_by_the_run_label(self): # the sweep of earlier runs filters by the key, the cleanup of this run by key and value self.assertEqual( @@ -435,5 +416,24 @@ class ScenarioSequenceTest(unittest.TestCase): ) +class ScenarioSequenceTest(unittest.TestCase): + """A role scenario previews the fresh host and the configured host with molecule's check step.""" + + def test_role_scenarios_run_check_before_and_after_converge(self): + # A check-only scenario has no converge to surround, so it is not held to this. + files = sorted(REPO.glob("roles/*/molecule/*/molecule.yml")) + self.assertTrue(files) + for path in files: + sequence = yaml.safe_load(path.read_text())["scenario"]["test_sequence"] + if "converge" not in sequence: + continue + at = sequence.index("converge") + self.assertEqual( + sequence[at - 1 : at + 3], + ["check", "converge", "check", "idempotence"], + path.relative_to(REPO), + ) + + if __name__ == "__main__": unittest.main()